1. 程式人生 > >SrpingBoot 中jackson格式化LocalDate、Date日期型別

SrpingBoot 中jackson格式化LocalDate、Date日期型別

摘要:

  最近在學習Springboot框架,在用jackson格式化實體類中Date和LocalDate時,遇到了一系列問題,在此做下記錄。

1.LocalDate

     從Java 8之後,Java.time包下,添加了日期處理類
     新增的日期jar主要有三種:
     java.time.LocalDate  ->只對年月日做出處理
     java.time.LocalTime  ->只對時分秒納秒做出處理
     java.time.LocalDateTime ->同時可以處理年月日和時分秒

     但是為啥還需要搞一套全新的處理日期和時間的API?是因為舊的java.util.Date比較難用:

     1.java.util.Date月份從0開始,一月是0,十二月是11.java.time.LocalDate月份和星期都改成了enum,可以有效防止用錯。

     2.java.util.Date和SimpleDateFormatter都不是執行緒安全的,而LocalDateLocalTime和最基本的String一樣,是不變型別,不但執行緒安全,而且不能修改。

2.LocalDate用法

// 取當前日期:
LocalDate today = LocalDate.now(); // -> 2018-12-5
// 根據年月日取日期,12月就是12:
LocalDate crischristmas = LocalDate.of(2018, 12, 05); // -> 2014-12-25
// 根據字串取:
LocalDate endOfFeb = LocalDate.parse("2018-12-05"); // 嚴格按照ISO yyyy-MM-dd驗證,02寫成2都不行,當然也有一個過載方法允許自己定義格式
LocalDate.parse("2018-12-05"); // 無效日期無法通過:DateTimeParseException: Invalid date

3. LocalDate日期轉換

// 取本月第1天:
LocalDate firstDayOfThisMonth = today.with(TemporalAdjusters.firstDayOfMonth()); // 2018-12-01
// 取本月第2天:
LocalDate secondDayOfThisMonth = today.withDayOfMonth(2); // 2018-12-02
// 取本月最後一天,再也不用計算是28,29,30還是31:
LocalDate lastDayOfThisMonth = today.with(TemporalAdjusters.lastDayOfMonth()); // 2018-12-31
// 取下一天:
LocalDate firstDayOf2019 = lastDayOfThisMonth.plusDays(1); // 變成了2019-01-01
// 取2019年1月第一個週一,這個計算用Calendar要死掉很多腦細胞:
LocalDate firstMondayOf2019 = LocalDate.parse("2019-01-01").with(TemporalAdjusters.firstInMonth(DayOfWeek.MONDAY)); // 2019-01-07

4.LocalTime用法 

//LocalTime到毫秒位
LocalTime now = LocalTime.now();//11:25:50.095
//去掉毫秒
now.withNano(0); //11:25:50
//用構造方法建立LocalTime
LocalTime.of(10, 10, 20);
//字串轉換
LocalTime.parse("12:00:00");

 5.JDBC中資料庫日期型別跟JAVA8的新型別關聯起來

SQL -> Java
--------------------------
date -> LocalDate
time -> LocalTime
timestamp -> LocalDateTime

6.SpringBoot中Controller中日期型別(Date、LocalDate)資料繫結

     1.添加註解@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date time,如果用的是實體類,那麼在實體類對應屬性上新增該註解。該註解是用於將request中日期型別的引數按照pattern格式格式化,然後賦值給對應屬性

7.SpringBoot中@ResponseBody註解Jackson 格式化輸出日期

      Date型別的屬性格式化有以下幾種方法:

     1.在bean物件對應日期屬性上新增@JsonFormat(pattern="yyyyMMdd")註解。

@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss")
private Date createTime;

      2.在apllication.yml加入下面配置就可以

#時間戳統一轉換
spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
  time-zone: GMT+8

     3. LocalDate、LocalDateTime、LocalTime型別資料 格式化:

      配置方案一:

             定義一個配置類,在其中定義兩個Bean,即可完成全域性日期格式化處理,同時還兼顧了Date和LocalDateTime並存。實體類中日期屬性不需要添加註解,Date型別屬性,會由application.yml中spring.jackson.date-format自動格式化,LocalDate型別的屬性,則會被如下定義的LocalDateSerializer 格式化

@Configuration
public class LocalDateTimeSerializerConfig {

	@Value("${spring.jackson.date-format}")
    private String pattern ;

     // 方案一
    @Bean 
    public LocalDateSerializer localDateDeserializer() { 
        return new LocalDateSerializer(DateTimeFormatter.ofPattern(pattern)); 
    }
      
    @Bean 
    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() { 
    	return builder -> builder.serializerByType(LocalDate.class, localDateDeserializer()); 
    }

}

      實體類 :

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	
	private Long id;
	
	private String name;
	
	private Integer age;
	
	@DateTimeFormat(pattern = "yyyyMMdd")
	private LocalDate birthday;

}

     Controller:

	@GetMapping("/{id}")
	public User getUser(@PathVariable Long id){
		return userService.getUserById(id);
	}

配置方案二

有時候,我們對日期格式要做特殊的處理,全域性的格式化方式無法滿足我們需求是,使用該方案是非常好的選擇,通過 @JsonFormat 註解我們可以更為精準的為日期欄位格式化,它的優先順序比方案一高,二者可結合使用

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	
	private Long id;
	
	private String name;
	
	private Integer age;
	
	@JsonFormat(pattern = "yyyyMMdd")
	private LocalDate birthday;

	public User(Long id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

}

配置方案三 

其實和第一種類似,只不過第一種的寫法更加優雅簡潔,如果有多種型別需要做統一格式化處理,這種方案也不是不可以考慮(經測試不能與方案二同時使用)

package com.carry.config;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

@Configuration
public class LocalDateTimeSerializerConfig {

    @Value("${spring.jackson.date-format:yyyy-MM-dd HH:mm:ss}")
    private String pattern;

    // 方案三
    @Bean
    @Primary
    public ObjectMapper serializingObjectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        objectMapper.registerModule(javaTimeModule);
        return objectMapper;
    }

    public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
        @Override
        public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
                throws IOException {
            gen.writeString(value.format(DateTimeFormatter.ofPattern(pattern)));
        }
    }

    public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
                throws IOException {
            return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(pattern));
        }
    }
}

 推薦使用方案一與方案二

 本文好多內容是參考了一下兩位大神的文章

參考資料地址:

https://www.liaoxuefeng.com/article/00141939241051502ada88137694b62bfe844cd79e12c32000

https://www.cnblogs.com/carrychan/p/9883172.html