springboot 时间戳和 数据库时间相差14个小时

在 springboot 开发过程当中遇到一个奇怪的问题,就是已经设置系统时间GMT+8, 可是时间到数据库后会减小14个小时。后来发现是 jvm 时区和数据库时区设置不一致的问题。html

jvm 设置的是 GMT+8,数据库是 CST 时区。CST 时区比较混乱,会在冬令时或夏令时致使相差 13 或 14 个小时,因此须要改为本身须要的。mysql

spring 开发过程当中时区设置spring

1 jvm 系统时区设置,在 application.yml 配置文件中sql

spring:
  jackson:
    date-format: yyyy-MM-dd HH:mm:ss
    time-zone: GMT+8

2 在请求参数中,使用 JsonFormat 配置解析规则数据库

import com.fasterxml.jackson.annotation.JsonFormat;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date birthday;

3 在数据库链接中设置时间的解析时区,该方法不须要 mysql 服务器设置时区而后重启了springboot

jdbc:mysql://localhost:3306/table_name?useUnicode=true&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=GMT%2B8

 

通过以上设置后请求时间戳和运行时时间戳和数据库时间戳就一致了。服务器

 

4 数据库查看时区命令app

show variables like '%time_zone%';

 

参考文献jvm

1 https://juejin.im/post/5902e087da2f60005df05c3dpost

2 https://www.cnblogs.com/jason1990/archive/2018/11/28/10032181.html

相关文章
相关标签/搜索