0%

springboot返回的时间时区不对

问题

import java.sql.Timestamp;
//对象中属性为
private Timestamp startTime;
private Timestamp endTime;

通过spring boot restful接口返回到前端,时区自动转换不对,晚八个小时:

{
    "id": 0,
    "startTime": "2020-04-29T06:15:00.000+0000",
    "endTime": "2020-04-29T06:15:00.000+0000",
    "taskResult": false
}

解决办法

application.yml中指定日期格式和时区

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: Asia/Shanghai

再次测试:

{
    "id": 0,
    "startTime": "2020-04-29 14:15:00",
    "endTime": "2020-04-29 14:15:00",
    "taskResult": false
}