1. 程式人生 > >Springboot配置時間格式

Springboot配置時間格式

方法一:
可以在apllication.property加入下面配置就可以

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

方法二:

  @JsonFormat(timezone = "GMT+8",pattern = "yyyy-MM-dd")
  @DateTimeFormat(pattern="yyyy-MM-dd")
  private Date createdDate;
@JsonFormat(timezone = "GMT+8", pattern = "yyyyMMddHHmmss"
) private Date createTime;

方法三:
可以在apllication.yml加入下面配置就可以

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

注意:
@JsonIgnoreProperties此註解是類註解,作用是json序列化時將java bean中的一些屬性忽略掉,序列化和反序列化都受影響。
@JsonIgnoreProperties(value = { "word" }) 。
@JsonIgnore此註解用於屬性或者方法上(最好是屬性上),作用和上面的@JsonIgnoreProperties

一樣。
@JsonSerialize此註解用於屬性或者getter方法上,用於在序列化時嵌入我們自定義的程式碼,比如序列化一個double時在其後面限制兩位小數點。
@JsonSerialize(using = CustomDoubleSerialize.class)
@JsonDeserialize此註解用於屬性或者setter方法上。
用於在反序列化時可以嵌入我們自定義的程式碼,類似於上面的
@JsonSerialize
@JsonDeserialize(using = CustomDateDeserialize.class)