开发者

springboot如何统一设置时区

开发者 https://www.devze.com 2023-01-07 10:39 出处:网络 作者: 阿良@
目录springboot 统一设置时区springboot mysql 时区问题总结寻找原因词义几种情况总结springboot 统一设置时区
目录
  • springboot 统一设置时区
  • springboot mysql 时区问题总结
    • 寻找原因
    • 词义
    • 几种情况
  • 总结

    springboot 统一设置时区

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

    @SpringBootApplication
    public class Application {
     public static void main(String[] args) {
      // 设置时区为上海时区,即东八区
      TimjavascripteZone.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:47php: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如何统一设置时区

    说明开发者_Python入门,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,&n编程客栈bsp; 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如何统一设置时区

    总结

    jackson.time-zhttp://www.devze.comone管前端到web服务器 转换的时区。

    如果是时间类型为datetime,serverTimeZone说了算。

    如果时间类型为timestamp

    1、当time_zone和serverTimeZone不统一情况,会话中,表中时间数据,按照serverTimeZone设定

    2、如果过程中,修改了time_zone变量,库中数据直接变更。但是不影响当前会话查询,新增数据,就会根据time_zone调整量,调整实际入库。

    3、SpringBoot重启,其实就是新会话。情况同上_1。

    以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

    0

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    关注公众号