Java日期轉(zhuǎn)換注解配置date?format時(shí)間失效
引言
今天生產(chǎn)上突然出現(xiàn)了列表日期錯誤,與數(shù)據(jù)庫實(shí)際差了8個小時(shí);
這下可尷尬了,之前迭代都是正常,肯定是那里出幺蛾子了,一頓排除,原來是新添加攔截器繼承WebMvcConfigurationSupport導(dǎo)致date-format時(shí)間格式失效了。
順帶學(xué)習(xí)了一下@JsonFormat和@DateTimeFormat的用法:
注解@JsonFormat主要是后臺到前臺的時(shí)間格式的轉(zhuǎn)換
注解@DateTimeFormat主要是前后到后臺的時(shí)間格式的轉(zhuǎn)換
一、@DateTimeFormat
主要解決前端時(shí)間控件傳值到后臺接收準(zhǔn)確的Date類屬性的問題,我們可以在需要接收的類中對應(yīng)的時(shí)間類型屬性上加上@DateTimeFormat注解,并在注解中加上pattern屬性。
// 出生年月日 @DateTimeFormat(pattern = "yyyy-MM-dd HH-mm-ss") private Date birthday;
二、@JsonFormat
該注解主要解決后臺從數(shù)據(jù)庫中取出時(shí)間類型賦予java對象的Date屬性值無法在前端以一定的日期格式來呈現(xiàn),默認(rèn)返回的是一個帶時(shí)區(qū)的格式串,不符合我們?nèi)粘R尸F(xiàn)的yyyy-MM-dd格式的日期。 同樣,我們在對應(yīng)的接收對象時(shí)間類型上加上@JsonFormat注解,并在注解中加上pattern屬性以及timezone屬性,例如:
// 出生年月日 @JsonFormat(locale="zh", timezone="GMT+8", pattern="yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd") private Date birthday;
pattern:是你需要轉(zhuǎn)換的時(shí)間日期的格式
timezone:是時(shí)間設(shè)置為東八區(qū),避免時(shí)間在轉(zhuǎn)換中有誤差;GMT+8表示我們以東八區(qū)時(shí)區(qū)為準(zhǔn)。
三、application.yml文件配置
1、數(shù)據(jù)庫配置時(shí)區(qū)serverTimezone=Asia/Shanghai或者serverTimezone=GMT%2B8
解決mysql從數(shù)據(jù)庫查詢的時(shí)間與實(shí)際時(shí)間相差8小時(shí):
spring.datasource.url=jdbc:mysql://10.35.105.25:3306/database?characterEncoding=utf-8&serverTimezone=GMT%2B8
2、配置spring.jackson
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss spring.jackson.time-zone=GMT+8
spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
四、SpringBoot攔截器
SpringBoot攔截器SpringBoot攔截器繼承WebMvcConfigurationSupport導(dǎo)致date-format時(shí)間格式失效
1、解決辦法不繼承WebMvcConfigurationSupport,修改為實(shí)現(xiàn)WebMvcConfigurer接口
@Configuration public class WebConfig implements WebMvcConfigurer { @Resource private JwtInterceptor jwtInterceptor; @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(jwtInterceptor) //攔截所有url .addPathPatterns("/**") //排除登錄url .excludePathPatterns("/", "/login"); } }
2、另外還有方式就是引入fastjson替換jackson
3、也可以繼承WebMvcConfigurationSupport重寫configureMessageConverters,自定義轉(zhuǎn)換器處理日期類型轉(zhuǎn)換
@Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder() .dateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")) .serializationInclusion(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_NULL); converters.add(new StringHttpMessageConverter(StandardCharsets.UTF_8)); converters.add(new MappingJackson2HttpMessageConverter(builder.build())); }
以上就是Java日期轉(zhuǎn)換注解配置date format時(shí)間失效的詳細(xì)內(nèi)容,更多關(guān)于Java日期轉(zhuǎn)換注解配置的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
阿里SpringBoot應(yīng)用自動化部署實(shí)現(xiàn)IDEA版Jenkins
這篇文章主要為大家介紹了阿里SpringBoot應(yīng)用自動化部署實(shí)現(xiàn)IDEA版Jenkins過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07SpringMVC REST風(fēng)格深入詳細(xì)講解
這篇文章主要介紹了SpringMVC REST風(fēng)格,Rest全稱為Representational State Transfer,翻譯為表現(xiàn)形式狀態(tài)轉(zhuǎn)換,它是一種軟件架構(gòu)2022-10-10hadoop client與datanode的通信協(xié)議分析
本文主要分析了hadoop客戶端read和write block的流程. 以及client和datanode通信的協(xié)議, 數(shù)據(jù)流格式等2012-11-11使用@JsonFormat和@DateTimeFormat對Date格式化操作
這篇文章主要介紹了使用@JsonFormat和@DateTimeFormat對Date格式化操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具
今天周末,給大家分享基于java代碼實(shí)現(xiàn)C盤文件統(tǒng)計(jì)工具,在這小編使用的版本是Maven-3.9.9,jdk1.8,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-07-07