本文最后更新于:几秒前
配置一下日期格式化, 留一个备忘录
以下为 Kotlin 版本, Java版本修改一样, 自行修改即可
Date 类型
1 2 3
| spring.jackson.default-property-inclusion=non_null spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8
|
Java8 LocalTime, LocalDate, LocalDateTime 类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.time.format.DateTimeFormatter
private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
@Bean fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer { return Jackson2ObjectMapperBuilderCustomizer { builder -> builder.serializerByType(LocalTime::class.java, LocalDateTimeSerializer(timeFormatter)) builder.deserializerByType(LocalTime::class.java, LocalDateTimeDeserializer(timeFormatter))
builder.serializerByType(LocalDateTime::class.java, LocalDateTimeSerializer(dateTimeFormatter)) builder.deserializerByType(LocalDateTime::class.java, LocalDateTimeDeserializer(dateTimeFormatter))
builder.serializerByType(LocalDateTime::class.java, LocalDateTimeSerializer(dateTimeFormatter)) builder.deserializerByType(LocalDateTime::class.java, LocalDateTimeDeserializer(dateTimeFormatter)) } }
|
Get查询参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| @Bean fun localDateTimeFormatter(): Formatter<LocalDateTime> { return object : Formatter<LocalDateTime> { override fun parse(text: String, locale: java.util.Locale): LocalDateTime { return LocalDateTime.parse(text, dateTimeFormatter) }
override fun print(t: LocalDateTime, locale: java.util.Locale): String { return t.format(dateTimeFormatter) } } }
@Bean fun localDateFormatter(): Formatter<LocalDate> { return object : Formatter<LocalDate> { override fun parse(text: String, locale: java.util.Locale): LocalDate { return LocalDate.parse(text, dateFormatter) }
override fun print(t: LocalDate, locale: java.util.Locale): String { return t.format(dateFormatter) } } }
@Bean fun localTimeFormatter(): Formatter<LocalTime> { return object : Formatter<LocalTime> { override fun parse(text: String, locale: java.util.Locale): LocalTime { return LocalTime.parse(text, timeFormatter) }
override fun print(t: LocalTime, locale: java.util.Locale): String { return t.format(timeFormatter) } } }
|
懒人版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| 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 org.springframework.format.Formatter import org.springframework.web.servlet.config.annotation.WebMvcConfigurer import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime import java.time.format.DateTimeFormatter
@Configuration class JacksonConfig : WebMvcConfigurer { private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss") private val dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") private val timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss")
@Bean fun jackson2ObjectMapperBuilderCustomizer(): Jackson2ObjectMapperBuilderCustomizer { return Jackson2ObjectMapperBuilderCustomizer { builder -> builder.serializerByType(LocalTime::class.java, LocalDateTimeSerializer(timeFormatter)) builder.deserializerByType(LocalTime::class.java, LocalDateTimeDeserializer(timeFormatter))
builder.serializerByType(LocalDateTime::class.java, LocalDateTimeSerializer(dateTimeFormatter)) builder.deserializerByType(LocalDateTime::class.java, LocalDateTimeDeserializer(dateTimeFormatter))
builder.serializerByType(LocalDateTime::class.java, LocalDateTimeSerializer(dateTimeFormatter)) builder.deserializerByType(LocalDateTime::class.java, LocalDateTimeDeserializer(dateTimeFormatter)) } }
@Bean fun localDateTimeFormatter(): Formatter<LocalDateTime> { return object : Formatter<LocalDateTime> { override fun parse(text: String, locale: java.util.Locale): LocalDateTime { return LocalDateTime.parse(text, dateTimeFormatter) }
override fun print(t: LocalDateTime, locale: java.util.Locale): String { return t.format(dateTimeFormatter) } } }
@Bean fun localDateFormatter(): Formatter<LocalDate> { return object : Formatter<LocalDate> { override fun parse(text: String, locale: java.util.Locale): LocalDate { return LocalDate.parse(text, dateFormatter) }
override fun print(t: LocalDate, locale: java.util.Locale): String { return t.format(dateFormatter) } } }
@Bean fun localTimeFormatter(): Formatter<LocalTime> { return object : Formatter<LocalTime> { override fun parse(text: String, locale: java.util.Locale): LocalTime { return LocalTime.parse(text, timeFormatter) }
override fun print(t: LocalTime, locale: java.util.Locale): String { return t.format(timeFormatter) } } } }
|
参考文章
SpringBoot中对LocalDateTime/LocalDate进行格式化并解析