四種Springboot常見全局時間格式化方式
背景
Springboot 全局時間格式化,前后端日期類型參數(shù)格式化。
解決
方式大部分四種
1 @JsonFormat
字段加上 @JsonFormat 注解后,LocalDateTime 和 Date 時間格式化成功。需要在每個字段上添加該注解,不算全局時間格式化。不推薦
import com.fasterxml.jackson.annotation.JsonFormat;
@Data public class TestDTO{ @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private LocalDateTime createTime; @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss") private Date updateTime; }
2 配置文件配置參數(shù)
這種方式只對 Date 類型生效。不推薦
spring: jackson: date-format: yyyy-MM-dd HH:mm:ss time-zone: GMT+8
3 @JsonComponent
自定義Configuration類。
Date、LocalDateTime類型日期均生效
推薦
/** * 全局日期格式化 如果某個字段不使用該格式 * 依舊可以使用 @JsonFormat(locale = "zh", timezone = "GMT+8", pattern = "yyyy-MM-dd") 修改某個字段的格式化信息,且@JsonFormat優(yōu)先級高于@JsonComponent配置的格式類型 */ @JsonComponent @Configuration public class DateFormatConfig { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; /** * 類型全局時間格式化 */ @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilder() { return builder -> { TimeZone tz = TimeZone.getTimeZone("GMT+8"); DateFormat df = new SimpleDateFormat(pattern); df.setTimeZone(tz); builder.failOnEmptyBeans(false) .failOnUnknownProperties(false) .featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .dateFormat(df); }; } /** * 類型全局時間格式化 */ @Bean public LocalDateTimeSerializer localDateTimeDeserializer() { return new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(pattern)); } @Bean public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { return builder -> builder.serializerByType(LocalDateTime.class, localDateTimeDeserializer()); } }
4 @Configuration
這種全局配置的實(shí)現(xiàn)方式與上邊的效果是一樣的。
注意:在使用此種配置后,字段手動配置@JsonFormat 注解將不再生效。
不推薦
@Configuration public class DateFormatConfig2 { @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}") private String pattern; public static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); @Bean @Primary public ObjectMapper serializingObjectMapper() { ObjectMapper objectMapper = new ObjectMapper(); JavaTimeModule javaTimeModule = new JavaTimeModule(); javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer()); javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer()); objectMapper.registerModule(javaTimeModule); return objectMapper; } /** * 時間類型裝換 */ @Component public class DateSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator gen, SerializerProvider provider) throws IOException { String formattedDate = dateFormat.format(date); gen.writeString(formattedDate); } } /** * 時間類型裝換 */ @Component public class DateDeserializer extends JsonDeserializer<Date> { @Override public Date deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException { try { return dateFormat.parse(jsonParser.getValueAsString()); } catch (ParseException e) { throw new RuntimeException("Could not parse date", e); } } } /** * 時間類型裝換 */ public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> { @Override public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern))); } } /** * 時間類型裝換 */ public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> { @Override public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException { return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern)); } } }
到此這篇關(guān)于四種Springboot常見全局時間格式化方式的文章就介紹到這了,更多相關(guān)Springboot全局時間格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot增加注解緩存@Cacheable的實(shí)現(xiàn)
這篇文章主要介紹了springboot增加注解緩存@Cacheable的實(shí)現(xiàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12mybatis Plus 多表聯(lián)合查詢的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis Plus 多表聯(lián)合查詢的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java數(shù)據(jù)結(jié)構(gòu)之對象比較詳解
這篇文章主要為大家詳細(xì)介紹了Java中對象的比較、集合框架中PriorityQueue的比較方式以及PriorityQueue的模擬實(shí)現(xiàn),感興趣的可以了解一下2022-07-07springboot實(shí)現(xiàn)發(fā)送郵件(QQ郵箱為例)
這篇文章主要為大家詳細(xì)介紹了springboot實(shí)現(xiàn)發(fā)送郵件,qq郵箱代碼實(shí)現(xiàn)郵件發(fā)送,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-06-06Java中ArrayIndexOutOfBoundsException 異常報錯的解決方案
本文主要介紹了Java中ArrayIndexOutOfBoundsException 異常報錯的解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的實(shí)戰(zhàn)案例
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)多數(shù)據(jù)源的實(shí)戰(zhàn)案例,文中通過示例代碼和圖文展示介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2024-01-01Java中的反射,枚舉及l(fā)ambda表達(dá)式的使用詳解
這篇文章主要為大家詳細(xì)介紹了Java的反射,枚舉及l(fā)ambda表達(dá)式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03