In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail how to format jackson Java8 dates in Springboot2. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
Import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonParser;import com.fasterxml.jackson.databind.*;import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;import org.apache.commons.lang3.StringUtils;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import org.springframework.core.convert.converter.Converter;import org.springframework.lang.NonNull;import java.io.IOException;import java.time.LocalDate Import java.time.LocalDateTime;import java.time.LocalTime;import java.time.format.DateTimeFormatter;import java.util.TimeZone;/** * jackson2 date serialization and deserialization * * / @ Configurationpublic class DateConverterConfig {private static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern ("yyyy-MM-dd HH:mm:ss"); private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern ("yyyy-MM-dd"); private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern ("HH:mm:ss") / * * jackson2 json serialized null field is output as an empty string * * @ return * @ author zhaoyang10 * / @ Bean @ Primary / / @ ConditionalOnMissingBean (ObjectMapper.class) public ObjectMapper serializingObjectMapper () {ObjectMapper objectMapper = new ObjectMapper (); JavaTimeModule javaTimeModule = new JavaTimeModule () / / No more unified processing Springcloud2 fegin will report date format error formatting date field directly add @ JsonFormat annotation processing / / serialize date format / / javaTimeModule.addSerializer (LocalDateTime.class, new LocalDateTimeSerializer ()); / / javaTimeModule.addSerializer (LocalDate.class, new LocalDateSerializer ()); / / javaTimeModule.addSerializer (LocalTime.class, new LocalTimeSerializer ()) / / deserialization date format / / javaTimeModule.addDeserializer (LocalDateTime.class, new LocalDateTimeDeserializer ()); / / javaTimeModule.addDeserializer (LocalDate.class, new LocalDateDeserializer ()); / / javaTimeModule.addDeserializer (LocalTime.class, new LocalTimeDeserializer ()); objectMapper.registerModule (javaTimeModule); objectMapper.disable (SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) ObjectMapper.disable (DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); objectMapper.setTimeZone (TimeZone.getTimeZone ("Asia/Shanghai")); return objectMapper;} / * * date serialization * / public class LocalDateTimeSerializer extends JsonSerializer {@ Override public void serialize (LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString (value.format (DATETIME_FORMATTER)) }} / * date deserialization * / public class LocalDateTimeDeserializer extends JsonDeserializer {@ Override public LocalDateTime deserialize (JsonParser p, DeserializationContext ctxt) throws IOException {return LocalDateTime.parse (p.getValueAsString (), DATETIME_FORMATTER) }} / * * date serialization * / public class LocalDateSerializer extends JsonSerializer {@ Override public void serialize (LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString (value.format (DATE_FORMATTER)) }} / * date deserialization * / public class LocalDateDeserializer extends JsonDeserializer {@ Override public LocalDate deserialize (JsonParser p, DeserializationContext ctxt) throws IOException {return LocalDate.parse (p.getValueAsString (), DATE_FORMATTER) }} / * * date serialization * / public class LocalTimeSerializer extends JsonSerializer {@ Override public void serialize (LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {gen.writeString (value.format (TIME_FORMATTER)) }} / * date deserialization * / public class LocalTimeDeserializer extends JsonDeserializer {@ Override public LocalTime deserialize (JsonParser p, DeserializationContext ctxt) throws IOException {return LocalTime.parse (p.getValueAsString (), TIME_FORMATTER) }} / * receive the conversion processing of the input date of the front end * * @ return * / @ Bean public Converter LocalDateTimeConvert () {return new Converter () {@ Override public LocalDateTime convert (@ NonNull String source) {LocalDateTime date = null If (StringUtils.isNotBlank (source)) {date = LocalDateTime.parse (source, DATETIME_FORMATTER);} return date;}} } @ Bean public Converter LocalDateConvert () {return new Converter () {@ Override public LocalDate convert (@ NonNull String source) {LocalDate date = null; if (StringUtils.isNotBlank (source)) {date = LocalDate.parse (source, DATE_FORMATTER);} return date }}; @ Bean public Converter LocalTimeConvert () {return new Converter () {@ Override public LocalTime convert (@ NonNull String source) {LocalTime time = null; if (StringUtils.isNotBlank (source)) {time = LocalTime.parse (source, TIME_FORMATTER) } return time;}};}} this is the end of sharing about how to format jackson Java8 dates in Springboot2. I hope the above content can be helpful to you 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.
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.