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í)現(xiàn)簡(jiǎn)單混合運(yùn)算計(jì)算器的示例代碼
這篇文章主要介紹了java圖形化界面實(shí)現(xiàn)簡(jiǎn)單混合運(yùn)算計(jì)算器的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
ES6學(xué)習(xí)筆記之新增數(shù)據(jù)類型實(shí)例解析
這篇文章主要介紹了ES6學(xué)習(xí)筆記之新增數(shù)據(jù)類型,結(jié)合實(shí)例形式分析了ES6數(shù)據(jù)解構(gòu)賦值、新增數(shù)據(jù)類型Set集合、新增數(shù)據(jù)類型Map、Symbol類型等相關(guān)原理與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01
Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼與示例解析
本文主要介紹Java實(shí)現(xiàn)二維碼QRCode的編碼和解碼,這里給大家一個(gè)小示例以便理解,有需要的小伙伴可以參考下2016-08-08
spring boot 項(xiàng)目利用Jenkins實(shí)現(xiàn)自動(dòng)化部署的教程詳解
這篇文章主要介紹了spring boot 項(xiàng)目利用Jenkins實(shí)現(xiàn)自動(dòng)化部署的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-07-07
解決IDEA創(chuàng)建maven項(xiàng)目時(shí)pom.xml沒有變藍(lán)的問題
這篇文章主要介紹了解決IDEA創(chuàng)建maven項(xiàng)目時(shí)pom.xml沒有變藍(lán)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

