springboot全局日期格式化的兩種方式
方式一是配置參數(shù)
參數(shù)配置的方式就是在json序列化的時(shí)候,當(dāng)字段為日期類型的時(shí)候的format類型,就相當(dāng)于在所有日期字段上加了一個(gè)注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss"),但是每個(gè)字段都加注解太麻煩,所以直接使用全局配置來實(shí)現(xiàn)
參數(shù)配置也分為兩種配置
第一種是yml的配置
spring: jackson: #參數(shù)意義: #JsonInclude.Include.ALWAYS 默認(rèn) #JsonInclude.Include.NON_DEFAULT 屬性為默認(rèn)值不序列化 #JsonInclude.Include.NON_EMPTY 屬性為 空(””) 或者為 NULL 都不序列化 #JsonInclude.Include.NON_NULL 屬性為NULL 不序列化 default-property-inclusion: ALWAYS time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss
第二中配置方式是properties文件配置
#jackson相關(guān)配置 spring.jackson.date-format = yyyy-MM-dd HH:mm:ss #時(shí)區(qū)必須要設(shè)置 spring.jackson.time-zone= GMT+8 #ALWAYS的意思是即時(shí)屬性為null,仍然也會(huì)輸出這個(gè)key,對(duì)應(yīng)yml里面的注釋里面的類型 spring.jackson.default-property-inclusion=ALWAYS
方式二是自定義轉(zhuǎn)換類
先定義一個(gè)string轉(zhuǎn)date的轉(zhuǎn)換類,需要實(shí)現(xiàn)convert接口
import org.apache.commons.lang.StringUtils; import org.springframework.core.convert.converter.Converter; import org.springframework.stereotype.Component; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 全局handler前日期統(tǒng)一處理 * @author * @date 2019-06-03 */ @Component public class DateConverterConfig implements Converter<String, Date> { private static final List<String> formarts = new ArrayList<>(4); private static final String YYYY_MM = "yyyy-MM"; private static final String YYYY_MM_DD = "yyyy-MM-dd"; private static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm"; private static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; static{ formarts.add(YYYY_MM); formarts.add(YYYY_MM_DD); formarts.add(YYYY_MM_DD_HH_MM); formarts.add(YYYY_MM_DD_HH_MM_SS); } @Override public Date convert(String source) { if (StringUtils.isBlank(source)) { return null; } source = source.trim(); if(source.matches("^\\d{4}-\\d{1,2}$")){ return parseDate(source, formarts.get(0)); }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")){ return parseDate(source, formarts.get(1)); }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")){ return parseDate(source, formarts.get(2)); }else if(source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")){ return parseDate(source, formarts.get(3)); }else { throw new IllegalArgumentException("Invalid false value '" + source + "'"); } } /** * 格式化日期 * @param dateStr String 字符型日期 * @param format String 格式 * @return Date 日期 */ private Date parseDate(String dateStr, String format) { Date date; try { DateFormat dateFormat = new SimpleDateFormat(format); date = dateFormat.parse(dateStr); } catch (Exception e) { throw new IllegalArgumentException(e.getLocalizedMessage()); } return date; } }
第二步是把這個(gè)轉(zhuǎn)換類添加到WebMvcConfigurationSupport
注意,當(dāng)用戶重新實(shí)現(xiàn)了WebMvcConfigurationSupport這個(gè)類之后,在yml中定義的靜態(tài)資源路徑啥的會(huì)失效,需要在這里再次添加一下靜態(tài)資源路徑
import org.springframework.context.annotation.Configuration; import org.springframework.format.FormatterRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class WebMvcConfig extends WebMvcConfigurationSupport { /** * 添加自定義的Converters和Formatters. */ @Override protected void addFormatters(FormatterRegistry registry) { registry.addConverter(new DateConverterConfig()); } /** * 如果繼承了WebMvcConfigurationSupport,則在yml中配置的相關(guān)內(nèi)容會(huì)失效。 需要重新指定靜態(tài)資源 * * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**") .addResourceLocations("classpath:/static/") .addResourceLocations("classpath:/resources/") .addResourceLocations("classpath:/META-INF/resources/"); super.addResourceHandlers(registry); } }
到此這篇關(guān)于springboot全局日期格式化的兩種方式的文章就介紹到這了,更多相關(guān)springboot全局日期格式化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)模式之外觀模式的實(shí)現(xiàn)方式
這篇文章主要介紹了Java設(shè)計(jì)模式之外觀模式的實(shí)現(xiàn)方式,外觀模式隱藏系統(tǒng)的復(fù)雜性,并向客戶端提供了一個(gè)客戶端可以訪問系統(tǒng)的接口,這種類型的設(shè)計(jì)模式屬于結(jié)構(gòu)型模式,它向現(xiàn)有的系統(tǒng)添加一個(gè)接口,來隱藏系統(tǒng)的復(fù)雜性,需要的朋友可以參考下2023-11-11mybatis同一張表多次連接查詢相同列賦值問題小結(jié)
這篇文章主要介紹了mybatis同一張表多次連接查詢相同列賦值問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下2017-01-01Spring中的EurekaServer啟動(dòng)詳解
這篇文章主要介紹了Spring中的EurekaServer啟動(dòng)詳解,初始化eureka,包含eureka集群的同步和發(fā)布注冊(cè),這個(gè)方法時(shí)重寫ServletContextListener#contextInitialized,是eureka啟動(dòng)的入口了,需要的朋友可以參考下2023-11-11SpringMVC實(shí)現(xiàn)前端后臺(tái)交互傳遞數(shù)據(jù)
本篇文章主要介紹了SpringMVC實(shí)現(xiàn)前端后臺(tái)傳遞數(shù)據(jù)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03Java?Valhalla?Project項(xiàng)目介紹
這篇文章主要介紹了Java?Valhalla?Project項(xiàng)目介紹,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Spring中的攔截器HandlerInterceptor詳細(xì)解析
這篇文章主要介紹了Spring中的攔截器HandlerInterceptor詳細(xì)解析,HandlerInterceptor 是 Spring 框架提供的一個(gè)攔截器接口,用于在請(qǐng)求處理過程中攔截和處理請(qǐng)求,需要的朋友可以參考下2024-01-01spring mybatis多數(shù)據(jù)源實(shí)例詳解
本文主要介紹sping mybatis多數(shù)據(jù)源處理,在開發(fā)過程中經(jīng)常會(huì)遇到多個(gè)數(shù)據(jù)庫(kù),這里給大家舉例說明如何處理,希望能幫助有需要的小伙伴2016-07-07一文詳解Spring?Boot可以同時(shí)處理多少請(qǐng)求
SpringBoot是一個(gè)流行的Java開發(fā)框架,它被廣泛用于構(gòu)建Web應(yīng)用程序,但是,開發(fā)人員通常會(huì)擔(dān)心它的性能問題,特別是在高負(fù)載條件下,Spring?Boot能夠同時(shí)處理多少請(qǐng)求是一個(gè)重要的問題,在本文中,我們將討論SpringBoot的請(qǐng)求處理能力,并介紹如何提高性能2023-10-10Spring Data + Thymeleaf 3 + Bo
本篇文章主要介紹了Spring Data + Thymeleaf 3 + Bootstrap 4 實(shí)現(xiàn)分頁器實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-05-05