问题:在前后端传递Json时时间传递不过来,并且格式混乱

如下图所示:

从数据库中获取的时间
Java中json前后端日期传递处理
传递给页面的时间
Java中json前后端日期传递处理

解决方案

@JsonFormat、@DateTimeFormat

Java中json前后端日期传递处理

用在属性上即可@JsonFormat(pattern = "yyyy-MM-dd"),pattern 指定前端传递的日期格式(例如:sj:2018-1-26),

import com.fasterxml.jackson.annotation.JsonFormat;

@JsonFormat(pattern = “yyyy-MM-dd”)
//@DateTimeFormat(pattern = “yyyy-MM-dd”)
private Date sj;

注意:如果要精确到小时,则需要@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8"), 因为中国时间和标准时间有8个小时差

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-27
  • 2021-11-29
  • 2021-05-29
  • 2023-04-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-09
  • 2022-12-23
  • 2021-05-21
  • 2022-01-31
  • 2021-11-14
  • 2022-02-20
相关资源
相似解决方案