SpringBoot日期转换配置

方式 一

在实体类上加@DatetimeFormat与@JsonFormat注解

//@DatetimeFormat:将前台日期字符串转换成Date格式(用在输入参数上) 
@DateTimeFormat(pattern="yyyy-MM-dd HH:mm:ss")
private Date myDate;

//@JsonFormat:将服务器端Date日期转换成指定字符串格式(用在输出结果上)
//需要指定timezone,否则会有时区的问题
@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date myDate;

方式二

在applicition.properties中添加如下配置(推荐该方式)

#时间戳统一转换(输入string转换date,输出date转换string)
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8

猜你喜欢

转载自blog.csdn.net/luo15242208310/article/details/112604996