您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Springboot2中如何進(jìn)行jackson Java8日期格式化處理,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
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 日期序列化和反序列化處理 * */ @Configuration public 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序列化 null字段輸出為空串 * * @return * @author zhaoyang10 */ @Bean @Primary //@ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); //不再做統(tǒng)一處理 Springcloud2 fegin 會(huì)報(bào)日期格式化錯(cuò)誤 格式化的日期字段直接加@JsonFormat 注解處理 //序列化日期格式 //javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); //javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer()); //javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer()); //反序列化日期格式 //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; } /** * 日期序列化 */ public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DATETIME_FORMATTER)); } } /** * 日期反序列化 */ public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { return LocalDateTime.parse(p.getValueAsString(), DATETIME_FORMATTER); } } /** * 日期序列化 */ public class LocalDateSerializer extends JsonSerializer<LocalDate> { @Override public void serialize(LocalDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DATE_FORMATTER)); } } /** * 日期反序列化 */ public class LocalDateDeserializer extends JsonDeserializer<LocalDate> { @Override public LocalDate deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { return LocalDate.parse(p.getValueAsString(), DATE_FORMATTER); } } /** * 日期序列化 */ public class LocalTimeSerializer extends JsonSerializer<LocalTime> { @Override public void serialize(LocalTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(TIME_FORMATTER)); } } /** * 日期反序列化 */ public class LocalTimeDeserializer extends JsonDeserializer<LocalTime> { @Override public LocalTime deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { return LocalTime.parse(p.getValueAsString(), TIME_FORMATTER); } } /** * 接收前端入?yún)⑷掌诘霓D(zhuǎn)換處理 * * @return */ @Bean public Converter<String, LocalDateTime> LocalDateTimeConvert() { return new Converter<String, LocalDateTime>() { @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<String, LocalDate> LocalDateConvert() { return new Converter<String, LocalDate>() { @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<String, LocalTime> LocalTimeConvert() { return new Converter<String, LocalTime>() { @Override public LocalTime convert(@NonNull String source) { LocalTime time = null; if (StringUtils.isNotBlank(source)) { time = LocalTime.parse(source, TIME_FORMATTER); } return time; } }; } }
關(guān)于Springboot2中如何進(jìn)行jackson Java8日期格式化處理就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。