问题描述
从后台(数据库)获取时间参数显示到前台,或从前台拿到时间参数数据存入后台(数据库),可能存在格式问题,致使时间参数变成一串时间戳……
解决办法
给实体对象的时间参数添加如下注解
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonFormat;
@JsonFormat(pattern = "yyyy-MM-dd", timezone = "GMT+8")
@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date releaseTime;
参数说明
@JsonFormat:解决从数据库中获取时间参数显示到前台后,变成时间戳的问题;
pattern = “yyyy-MM-dd”:时间日期转化格式;
timezone = “GMT+8”:设置时区,设为东八区(避免时间在转换中有误差);
@DateTimeFormat:解决从前台获取时间数据传到后台的一些时间日期格式问题;