In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you SpringBoot how to achieve LocalDateTime date conversion, I hope you will learn something after reading this article, let's discuss it together!
First, two problems of LocalDateTime date conversion
1. String is converted to Date when parameters are passed in the frontend.
Org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 12 purse 48 13' could not be parsed at index 10 Nested exception is com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.time.LocalDateTime` from String "2020-02-11 12:48:13": Failed to deserialize java.time.LocalDateTime: (java.time.format.DateTimeParseException) Text '2020-02-11 12 purse 48 13' could not be parsed at index 10 at [Source: (PushbackInputStream); line: 4, column: 19] (through reference chain: com.example.demo.model.Test ["localDateTime"])
two。 Date is converted to String when the backend returns a value
There is a "T" in the middle of the LocalDateTime, and the time formats of other time types are not readable.
II. Solutions
Write a time configuration class that is formatted using the HTTP message converter.
Import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter / * uniform time configuration * / @ Configurationpublic class DateConfig {@ Bean public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer () {JavaTimeModule module = new JavaTimeModule (); module.addDeserializer (LocalDateTime.class, new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss")); module.addSerializer (LocalDateTime.class, new LocalDateTimeSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss") Return builder-> {builder.simpleDateFormat ("yyyy-MM-dd hh:mm:ss"); builder.deserializers (new LocalDateTimeDeserializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); builder.serializers (new LocalDateTimeSerializer (DateTimeFormatter.ofPattern ("yyyy-MM-dd hh:mm:ss"); builder.modules (module);};}} third, verification effect
1. The first problem solving effect picture:
two。 The second problem solving effect picture:
We can see that even the Date type is formatted properly after using the HTTP message converter for formatting.
After reading this article, I believe you have a certain understanding of "SpringBoot how to achieve LocalDateTime date conversion". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.