Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How SpringBoot formats LocalTime and LocalDate and LocalDateTime

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how SpringBoot formats LocalTime and LocalDate as well as LocalDateTime. The content is very detailed. Interested friends can refer to it. I hope it will be helpful to you.

The beginning

I haven't updated this collection for a long time. I forgot when the last update was. I originally planned to write more than a dozen articles in the Spring Boot series, but now I have only written the seventh article (including this article). I will continue to update it later. I have always felt that the main purpose of writing a blog is to sort out my technology stack, and sharing is only an accessory value, so I still have to stick to it.

Let's talk about how to format the time type of the Restful interface. Starting with JDK8, it provides more convenient date-time API, such as LocalTime, LocalDate and LocalDateTime, so that you can stay away from the original Date and Calendar classes, and date operation becomes much easier.

SpringBoot date formatting problem

However, when using these date-time types in Spring Boot's Restful interface development, you will find that it is invalid to use jackson's spring.jackson.date-format configuration for date type formatting. Why?

/ / after `spring.jackson.date-format=yyyy-MM-dd HH:mm: SS` is configured, the / / API returns the contents of LocalDateTime and Date. You will find that the Date type has been formatted, / / but LocalDateTime does not. {"localDateTime": "2019-07-14T12:20:23.615", "date": "2019-07-14 04:20:23"}

In fact, when Jackson serializes, different Serializer is called for serialization based on different types, and if there is no default Serializer, the class's toString () is called. Date type serialization uses DateSerializer, which uses the configuration in spring.jackson.date-format, while LocalDateTime uses DateTimeFormatter.ISO_LOCAL_DATE_TIME by default through LocalDateTimeSerializer,LocalDateTimeSerializer, so the above result is returned.

How to deal with it?

As you can see from the above, if you can override the default LocalDateTimeSerializer, you can format the date according to your needs.

This can be achieved in the following ways:

Customize the Bean of Jackson2ObjectMapperBuilderCustomizer, and specify the serialization and deserialization classes of LocalDate, LocalDateTime and LocalTime respectively (define the corresponding DatetimeFormatter as needed). The reference code is as follows:

Import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration Import org.springframework.context.annotation.Primary;import java.time.LocalDate;import java.time.LocalDateTime;import java.time.LocalTime;import java.time.format.DateTimeFormatter;/** * @ Author Cent.Chen 2019-07-14 * @ Description Dateformat Configuration. * / @ Configurationpublic class DateformatConfig {/ * Date format string * / private static final String DATE_FORMAT = "yyyy-MM-dd"; / * DateTime format string * / private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss"; / * * Time format string * / private static final String TIME_FORMAT = "HH:mm:ss" / * * Custom Bean * * @ return * / @ Bean @ Primary public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer () {return builder-> builder.serializerByType (LocalDateTime.class, new LocalDateTimeSerializer (DateTimeFormatter.ofPattern (DATETIME_FORMAT) .serializerByType (LocalDate.class, new LocalDateSerializer (DateTimeFormatter.ofPattern (DATE_FORMAT) .serializerByType (LocalTime.class New LocalTimeSerializer (DateTimeFormatter.ofPattern (TIME_FORMAT) .cubializerByType (LocalDateTime.class, new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern (DATETIME_FORMAT) .cubializerByType (LocalDate.class, new LocalDateDeserializer (DateTimeFormatter.ofPattern (DATE_FORMAT) .uploializerByType (LocalTime.class, new LocalTimeDeserializer (DateTimeFormatter.ofPattern (TIME_FORMAT) }}

Restart the service and test it again. The format date returned is as follows:

{"localDateTime": "2019-07-14 12:33:11", "date": "2019-07-14 04:33:11"} that's all about how SpringBoot formats LocalTime and LocalDate and LocalDateTime. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report