SpringBoot中時(shí)間類型 序列化、反序列化、格式處理示例代碼
【SpringBoot】 中時(shí)間類型 序列化、反序列化、格式處理
Date
yml全局配置
spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss #配置POST請(qǐng)求Body中Date時(shí)間類型序列化格式處理,并返回
請(qǐng)求參數(shù)類型轉(zhuǎn)換
/**
* 時(shí)間Date轉(zhuǎn)換
* 配置GET請(qǐng)求,Query查詢Date時(shí)間類型參數(shù)轉(zhuǎn)換
*/
@Component
public class DateConverter implements Converter<String, Date> {
@Override
public Date convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
return parseDate(source.trim(), "yyyy-MM-dd");
}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
return parseDate(source.trim(), "yyyy-MM-dd HH:mm:ss");
}
throw new IllegalArgumentException("Invalid value '" + source + "'");
}
public Date parseDate(String dateStr, String format) {
Date date = null;
try {
date = new SimpleDateFormat(format).parse(dateStr);
} catch (ParseException e) {
log.warn("轉(zhuǎn)換{}為日期(pattern={})錯(cuò)誤!", dateStr, format);
}
return date;
}
}
JDK8-時(shí)間類型-LocalDateTime、LocalDate、LocalTime
/**
* 序列化,反序列化,格式處理
*
* @author zc
* @date 2020/7/9 01:42
*/
@Slf4j
@Configuration
public class JacksonCustomizerConfig {
@Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
private String localDateTimePattern;
@Value("${spring.jackson.local-date-format:yyyy-MM-dd}")
private String localDatePattern;
@Value("${spring.jackson.local-time-format:HH:mm:ss}")
private String localTimePattern;
@Bean
public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
return builder -> {
builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
builder.serializerByType(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern(localDatePattern)));
builder.serializerByType(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern(localTimePattern)));
builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern(localDateTimePattern)));
builder.deserializerByType(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern(localDatePattern)));
builder.deserializerByType(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern(localTimePattern)));
};
}
/**
* 時(shí)間LocalDateTime轉(zhuǎn)換
*/
@Component
public static class LocalDateTimeConverter implements Converter<String, LocalDateTime> {
@Override
public LocalDateTime convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
return LocalDateTime.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
}
throw new IllegalArgumentException("Invalid value '" + source + "'");
}
}
/**
* 時(shí)間LocalDate轉(zhuǎn)換
*/
@Component
public static class LocalDateConverter implements Converter<String, LocalDate> {
@Override
public LocalDate convert(String source) {
if (StringUtils.isBlank(source)) {
return null;
}
if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
return LocalDate.parse(source, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
throw new IllegalArgumentException("Invalid value '" + source + "'");
}
}
}
趙小胖個(gè)人博客:https://zc.happyloves.cn:4443/wordpress/
PS:springboot 時(shí)間類型配置
springboot 自帶了jackson來(lái)處理時(shí)間,但不支持jdk8 LocalDate、LocalDateTime的轉(zhuǎn)換。
對(duì)于Calendar、Date二種日期,轉(zhuǎn)換方式有二種:
一、統(tǒng)一application.properties屬性配置文件中加入
spring.jackson.dateFormat=yyyy-MM-dd HH:mm:ss
如果你使用了joda第三包下的時(shí)間類型,
spring.jackson.jodaDateTimeFormat=yyyy-MM-dd HH:mm:ss
此方法為全局格式,沒(méi)辦法處理差異化。
二、使用jackson的時(shí)間注解@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
需要在每個(gè)日期類型上都添加,增加代碼量,但更靈活性。
總結(jié)
到此這篇關(guān)于SpringBoot中時(shí)間類型 序列化、反序列化、格式處理示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot中時(shí)間類型 序列化、反序列化、格式處理內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Jackson配置全局時(shí)間日期格式
- springboot 返回json格式數(shù)據(jù)時(shí)間格式配置方式
- SpringBoot中@Pattern注解對(duì)時(shí)間格式校驗(yàn)方式
- SpringBoot利用jackson格式化時(shí)間的三種方法
- springboot2.0 配置時(shí)間格式化不生效問(wèn)題的解決
- 關(guān)于Springboot日期時(shí)間格式化處理方式總結(jié)
- springboot json時(shí)間格式化處理的方法
- springboot項(xiàng)目中統(tǒng)一時(shí)間格式處理方法
相關(guān)文章
springboot+vue實(shí)現(xiàn)七牛云頭像的上傳
本文將介紹如何在Spring Boot項(xiàng)目中利用七牛云進(jìn)行圖片上傳并將圖片存儲(chǔ)在云存儲(chǔ)中,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
Java Swing JButton按鈕的實(shí)現(xiàn)示例
這篇文章主要介紹了Java Swing JButton按鈕的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
JAVA使用HtmlUnit爬蟲(chóng)工具模擬登陸CSDN案例
今天小編就為大家分享一篇關(guān)于JAVA使用HtmlUnit爬蟲(chóng)工具模擬登陸CSDN案例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
Hibernate中使用HQLQuery查詢?nèi)繑?shù)據(jù)和部分?jǐn)?shù)據(jù)的方法實(shí)例
今天小編就為大家分享一篇關(guān)于Hibernate中使用HQLQuery查詢?nèi)繑?shù)據(jù)和部分?jǐn)?shù)據(jù)的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Java mysql詳細(xì)講解雙數(shù)據(jù)源配置使用
在開(kāi)發(fā)過(guò)程中我們常常會(huì)用到兩個(gè)數(shù)據(jù)庫(kù),一個(gè)數(shù)據(jù)用來(lái)實(shí)現(xiàn)一些常規(guī)的增刪改查,另外一個(gè)數(shù)據(jù)庫(kù)用來(lái)實(shí)時(shí)存數(shù)據(jù)。進(jìn)行數(shù)據(jù)的統(tǒng)計(jì)分析??梢宰x寫分離??梢愿玫膬?yōu)化和提高效率;或者兩個(gè)數(shù)據(jù)存在業(yè)務(wù)分離的時(shí)候也需要多個(gè)數(shù)據(jù)源來(lái)實(shí)現(xiàn)2022-06-06
Java?NIO實(shí)現(xiàn)聊天系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java?NIO實(shí)現(xiàn)聊天系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題
下面小編就為大家分享一篇解決SpringMVC Controller 接收頁(yè)面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實(shí)現(xiàn)session共享及唯一登錄示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04

