@JsonFormat 使用方法

发布时间 2023-09-22 15:09:12作者: 渴望sunshine

@JsonFormat(shape =JsonFormat.Shape.STRING,pattern ="yyyy-MM-dd HH:mm:ss",timezone ="GMT+8")
private LocalDate createTime;

错误示范:

@JsonFormat(pattern ="yyyy-MM-dd HH:mm:ss")
private LocalDate createTime;

@JsonFormat注解是一个时间格式化注解,比如我们存储在mysql中的数据是date类型的,当我们读取出来封装在实体类中的时候,就会变成英文时间格式,而不是yyyy-MM-dd HH:mm:ss这样的中文时间,因此我们需要用到JsonFormat注解来格式化我们的时间。
  简而言之,将数据库中的data类型的时间格式化为yyyy-MM-dd HH:mm:ss。

  • shape: 表示序列化后的一种类型,枚举(详见后)

  • pattern: 表示日期的格式

  • timezone: 默认是GMT,中国需要GMT+8

  • locale: 根据位置序列化的一种格式