SpringBoot中時間類型 序列化、反序列化、格式處理示例代碼
【SpringBoot】 中時間類型 序列化、反序列化、格式處理
Date
yml全局配置
spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd HH:mm:ss #配置POST請求Body中Date時間類型序列化格式處理,并返回
請求參數(shù)類型轉(zhuǎn)換
/**
* 時間Date轉(zhuǎn)換
* 配置GET請求,Query查詢Date時間類型參數(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={})錯誤!", dateStr, format);
}
return date;
}
}
JDK8-時間類型-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)));
};
}
/**
* 時間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 + "'");
}
}
/**
* 時間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 + "'");
}
}
}
趙小胖個人博客:https://zc.happyloves.cn:4443/wordpress/
PS:springboot 時間類型配置
springboot 自帶了jackson來處理時間,但不支持jdk8 LocalDate、LocalDateTime的轉(zhuǎn)換。
對于Calendar、Date二種日期,轉(zhuǎn)換方式有二種:
一、統(tǒng)一application.properties屬性配置文件中加入
spring.jackson.dateFormat=yyyy-MM-dd HH:mm:ss
如果你使用了joda第三包下的時間類型,
spring.jackson.jodaDateTimeFormat=yyyy-MM-dd HH:mm:ss
此方法為全局格式,沒辦法處理差異化。
二、使用jackson的時間注解@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss")
需要在每個日期類型上都添加,增加代碼量,但更靈活性。
總結
到此這篇關于SpringBoot中時間類型 序列化、反序列化、格式處理示例代碼的文章就介紹到這了,更多相關SpringBoot中時間類型 序列化、反序列化、格式處理內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot+vue實現(xiàn)七牛云頭像的上傳
本文將介紹如何在Spring Boot項目中利用七牛云進行圖片上傳并將圖片存儲在云存儲中,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-08-08
Java Swing JButton按鈕的實現(xiàn)示例
這篇文章主要介紹了Java Swing JButton按鈕的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-12-12
Hibernate中使用HQLQuery查詢?nèi)繑?shù)據(jù)和部分數(shù)據(jù)的方法實例
今天小編就為大家分享一篇關于Hibernate中使用HQLQuery查詢?nèi)繑?shù)據(jù)和部分數(shù)據(jù)的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03
Java mysql詳細講解雙數(shù)據(jù)源配置使用
在開發(fā)過程中我們常常會用到兩個數(shù)據(jù)庫,一個數(shù)據(jù)用來實現(xiàn)一些常規(guī)的增刪改查,另外一個數(shù)據(jù)庫用來實時存數(shù)據(jù)。進行數(shù)據(jù)的統(tǒng)計分析??梢宰x寫分離??梢愿玫膬?yōu)化和提高效率;或者兩個數(shù)據(jù)存在業(yè)務分離的時候也需要多個數(shù)據(jù)源來實現(xiàn)2022-06-06
解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題
下面小編就為大家分享一篇解決SpringMVC Controller 接收頁面?zhèn)鬟f的中文參數(shù)出現(xiàn)亂碼的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03
SpringBoot+SpringSession+Redis實現(xiàn)session共享及唯一登錄示例
這篇文章主要介紹了SpringBoot+SpringSession+Redis實現(xiàn)session共享及唯一登錄示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-04-04

