springboot如何统一设置时区

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

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

标签:springboot,统一设置,时区
0
投稿

猜你喜欢

  • 详解java构建者模式Builder

    2021-11-16 18:49:32
  • 最小树形图模板朱刘算法分享

    2023-11-07 07:04:38
  • Java热门笔试试题整理

    2023-11-25 08:56:33
  • Maven引入本地Jar包并打包进War包中的方法

    2023-06-16 12:43:28
  • springboot嵌套子类使用方式—前端与后台开发的注意事项

    2023-09-16 12:37:22
  • 详解Flutter中视频播放器插件的使用教程

    2023-06-15 23:47:31
  • Java 日期转换详解及实例代码

    2021-06-30 02:15:43
  • Java List分页功能实现代码实例

    2022-06-02 13:56:14
  • SpringBoot2使用Jetty容器操作(替换默认Tomcat)

    2023-11-24 01:17:15
  • 详解Java类型擦除机制

    2023-10-29 06:41:21
  • 详解java接口(interface)在不同JDK版本中的变化

    2022-07-18 03:19:16
  • Java面试题-实现复杂链表的复制代码分享

    2023-11-23 20:05:39
  • jQuery 动画效果代码分享

    2023-11-24 00:10:12
  • JavaWeb验证码校验功能代码实例

    2022-07-18 23:46:52
  • java实现简单的英文文本单词翻译器功能示例

    2023-11-28 10:22:15
  • Redis6搭建集群并在SpringBoot中使用RedisTemplate的实现

    2023-10-31 14:48:05
  • SpringBoot中实现分布式的Session共享的详细教程

    2023-08-23 18:23:43
  • java生成图片验证码功能

    2023-06-27 00:31:55
  • Springboot上传文件时提示405问题及排坑过程

    2022-12-13 03:03:58
  • Java构建JDBC应用程序的实例操作

    2023-08-07 12:09:13
  • asp之家 软件编程 m.aspxhome.com