利用ORM数据访问层框架,查询出来的数据绑定到对象上,对于时间类型会绑定到Date类型,
我们知道Date类型的数据转成json会自动转化为时间戳,如果我们想转化为字符串怎么办?
1、利用SimpleDateFormat
new SimpleDateFormat(“yyyyMMdd”).format(date);
2.利用fastjson的@JSONField注解
“`
@JSONField(format=”yyyyMMddHHmmss”)
private Date sendTime;//拆票后的入库时间
@JSONField(format=”yyyyMMddHHmmss”)
private Date dealTime;//出票时间
3.利用注解@DateTimeFormat、@JsonFormat
@DatetimeFormat是将String转换成Date,一般前在前端数据绑定VO时用到
@JsonFormat(pattern=”yyyy-MM-dd”) 将Date转换成String一般在数据绑定DTO,传递给前端时用到
@DateTimeFormat(pattern=”yyyy-MM-dd HH:mm:ss”)
@JsonFormat(pattern=”yyyy-MM-dd HH:mm:ss”,timezone = “GMT+8”)
private Date upddate;