前端时间:Tue Nov 26 2019 23:59:00 GMT+0800 (中国标准时间),后台接收到的时间为:2019-11-26T15:59:00.000Z;两个时间相差8小时,也就是说这两个时间是不同时区的,2019-11-26T15:59:00.000Z是(UTC)

在网上找到很多解决方案,如下所示:(但是都没能解决我的问题

1、在启动类加上

@PostConstruct

void setDefaultTimezone() {
 TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
} 
2、在application.properties加上
 ## json setting
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=Asia/Shanghai 
 
3、在启动类 启动run方法里加上
public static void main(String[] args) {
   TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
  SpringApplication.run(BaseMicroServiceApplication.class, args);
}

我的解决方案是,在前端js把时间戳(Tue Nov 26 2019 23:59:00 GMT+0800)转为日期格式(2019-11-26 23:59:00)。(怪我前端没学好,捣鼓了挺久才把问题解决)

        var begin = $scope.beginQuery;
        var end = $scope.endQuery;
        function formatDate(date) {
            return moment(date).format('YYYY-MM-DD HH:mm:ss');
          }
         console.log(formatDate(begin));
         console.log(end);

输出日期为:

spring boot项目前端和后台日期参数时区不一致问题

 

 注:第一个是转换后的日期,第二是没转换的时间戳

问题解决了,就是后台接收时间戳的时间把时区转为了UTC。

但是至于我为什么用前3种方式,没能解决问题,可能是我后台接收参数的方式不是json;

在这里做一下总结,下次再遇到可以都试一下

相关文章:

  • 2021-09-04
  • 2021-08-15
  • 2021-10-23
  • 2023-03-17
  • 2021-05-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-26
  • 2022-12-23
  • 2021-12-01
  • 2022-12-23
  • 2021-10-14
  • 2021-08-29
相关资源
相似解决方案