软件编程
位置:首页>> 软件编程>> java编程>> springboot如何统一设置时区

springboot如何统一设置时区

作者:阿良@  发布时间:2022-01-26 18:16:39 

标签:springboot,统一设置,时区

springboot 统一设置时区

控制springboot服务的时区为东八区

@SpringBootApplication
public class Application {
  public static void main(String[] args) {
    // 设置时区为上海时区,即东八区
    TimeZone.setDefault(TimeZone.getTimeZone(ZoneId.SHORT_IDS.get("CTT")));
    SpringApplication.run(Application.class, args);
  }
}

以下是测试例子

springboot 启动时候设置时区,如下代码所示

@SpringBootApplication
public class EwPbServerApplication {

    public static void main(String[] args) {
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
        TimeZone.setDefault(timeZone);
        SpringApplication.run(EwPbServerApplication.class, args);
    }
}

测试请求接口获取时间

    @GetMapping("test")
    @ApiOperation(value = "测试时间", httpMethod = "GET")
    public void test() {
        //当前时间为 2022-09-06 17:46
        //启动类设置时区后,获取当前时间
        Date date = new Date();
        DateTime date1 = DateUtil.date();
        LocalDateTime localDateTime = LocalDateTime.now();
        //设置时区为-东八区
        LocalDateTime.now(ZoneId.of("Asia/Shanghai"))

        log.info("date=={}", date);
        log.info("date1=={}", date1);
        log.info("localDateTime=={}", localDateTime);
        log.info("now=={}", now);
//        2022-09-06 09:47:01.385  xxxx  : date==Tue Sep 06 09:47:01 UTC 2022
//        2022-09-06 09:47:01.385  xxxx  : date1==2022-09-06 09:47:01
//        2022-09-06 09:47:01.386  xxxx  : localDateTime==2022-09-06T09:47:01.381
//        2022-09-06 09:47:01.386  xxxx  : now==2022-09-06T17:47:01.385
    }

由测试结果得知,springboot 启动时设置时区之后全局生效,但是优先级小于手动设置

springboot mysql 时区问题总结

寻找原因

后端开发中常见的几个时区设置

第一个设置点配置文件   spring.jackson.time-zone

第二个设置点 高版本SpringBoot版本 mysql-connector-java 用的是8.X,mysql8.X的jdbc升级了,增加了时区(serverTimezone)属性,并且不允许为空。

第三个设置点 mysql  time_zone变量

词义

serverTimezone临时指定mysql服务器的时区

spring.jackson.time-zone  设置spring默认时区

system_time_zone mysql服务器时区 ,time_zone默认System追随system_time_zone

几种情况

1、time_zone 为 System,serverTimezone为GMT+8,jackson.time-zone未定义

springboot如何统一设置时区

插入情况

springboot如何统一设置时区

springboot如何统一设置时区

springboot如何统一设置时区

再查询此条记录

springboot如何统一设置时区

个人觉得Spring默认时区为格林尼治时区,web服务器当前时区为东八区,进行加8操作。

2、set GLOBAL time_zone = '+3:00',serverTimezone为GMT+8,jackson.time-zone为GMT+8

createTime 为 timestamp类型

springboot如何统一设置时区

修改配置后,需要重启SpringBoot

新增情况

springboot如何统一设置时区

数据库中显示

springboot如何统一设置时区

查询记录

springboot如何统一设置时区

个人理解,serverTimezone设置覆盖掉了mysql的time_zone变量,跟SpringBoot会话时区还是东8

3、上述环境,不重启SpringBoot,直接改变time_zone = '+5:00'

改变后,上条记录往后调整2小时。

springboot如何统一设置时区

SpringBoot查询,一样

springboot如何统一设置时区

说明,timeStamp类型存储的是格林尼治时间,加上time_zone时区

当time_zone变化时,会话没结束,serverTimeZone东8还是对应time_zone的东3

SpringBoot插入

springboot如何统一设置时区

springboot如何统一设置时区

个人理解,serverTimeZone东8 还是和 time_zone 东3对应,但是插入发现 当前time_zone已经改成东5,就加2小时。

重启SpringBoot,重新查询

springboot如何统一设置时区

springboot如何统一设置时区

虽然,mysql变量time_zone为+5,但是重启后,serverTimeZone直接覆盖,设置时间区间为东8

重新把time_zone改回东3

springboot如何统一设置时区

改回重新打开表,发现又回来了

不启动SpringBoot,查询数据,还是老样子

springboot如何统一设置时区

此时,添加一条数据。

springboot如何统一设置时区

往前推了2小时。

springboot如何统一设置时区

SpringBoot查询

springboot如何统一设置时区

重启SpringBoot,查出来就是库中数据。

springboot如何统一设置时区

4、serverTimezone为GMT,jackson.time-zone为GMT+8,time_zone为东3

springboot如何统一设置时区

springboot如何统一设置时区

springboot如何统一设置时区

serverTimeZone为格林尼治时间,web服务器为东八,所以直接推迟8小时

springboot如何统一设置时区

取出来刚好反一下,显示正常。

此时,修改serverTimeZone为东八。

springboot如何统一设置时区

5、时间字段类型为timestamp,使用默认current_timestamp,  serverTimezone为GMT,jackson.time-zone为GMT+8,time_zone为东3

因mysql时区东三时间为

springboot如何统一设置时区

插入后数据为

springboot如何统一设置时区

但是serverTimeZone为格林尼治时间,jackson.time-zone为东八,加8小时

springboot如何统一设置时区

6、时间字段类型为datetime,serverTimezone为GMT+8,jackson.time-zone为GMT+8,time_zone为东3

插入

springboot如何统一设置时区

库中

springboot如何统一设置时区

查询

springboot如何统一设置时区

time_zone从东3修改为东5

重新打开库

springboot如何统一设置时区

不启动SpringBoot

springboot如何统一设置时区

重启SpringBoot,还是一样。

修改serverTimeZone为GMT,其他不改动

springboot如何统一设置时区

springboot如何统一设置时区

查询

springboot如何统一设置时区

来源:https://blog.csdn.net/AA8310888193aaa/article/details/126769929

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com