zoukankan      html  css  js  c++  java
  • springboot 时间戳和 数据库时间相差14个小时

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

    jvm 设置的是 GMT+8,数据库是 CST 时区。CST 时区比较混乱,会在冬令时或夏令时导致相差 13 或 14 个小时,所以需要改成自己需要的。

    spring 开发过程中时区设置

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

    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 服务器设置时区然后重启了

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

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

    4 数据库查看时区命令

    show variables like '%time_zone%';

    参考文献

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

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

  • 相关阅读:
    Some interesting problems in Group theory
    Abstract Harmonic Analysis Lecture Notes
    郭懋正《实变函数与泛函分析》课后题答案
    江泽坚《泛函分析》第二版 参考答案
    国外著名数学杂志
    郑继明等《数值分析》习题解答
    《摩诃般若波罗蜜多心经》新译 何新 译
    炼静
    金刚经原文
    道德经全文
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/12124973.html
Copyright © 2011-2022 走看看