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

  • 相关阅读:
    scrapy框架持久化存储 以及响应参数
    scrapy框架
    12306 模拟登陆
    Python第三库---requests库详解
    批处理-----4.for循环中的变量
    批处理-----3. for循环详解
    批处理-----2.常用特殊符号
    批处理-----1.内部命令简介
    Python第三方库---requests库
    Python 自带标准库
  • 原文地址:https://www.cnblogs.com/zhaopengcheng/p/12124973.html
Copyright © 2011-2022 走看看