jdbc连接mysq之serverTimezone设定方式

 更新时间:2024年01月27日 09:15:49   作者:喵主子  
这篇文章主要介绍了jdbc连接mysq之serverTimezone设定方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

jdbc连接mysq之serverTimezone设定

mysql返回的时间总是有问题,比实际时间要早8小时。

检查是jdbc连接的url中配置的时区有问题,原先是

jdbc:mysql://172.xx.xx.xx:3306/test?useUnicode=true&characterEncoding=gbk&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

尝试了GMT、GMT8、UTC+8,UTC0800等可能的参数均报错。

后来在mysql文档中发现可用的时区都在/usr/share/zoneinfo目录下

ll下发现可用的时区信息

如下:

lewis@lewis-dzwww:/usr/share/zoneinfo$ ll
总用量 308
drwxr-xr-x  21 root root  4096 5月  27 12:54 ./
drwxr-xr-x 334 root root 12288 6月  21 15:11 ../
drwxr-xr-x   2 root root  4096 5月  27 12:56 Africa/
drwxr-xr-x   6 root root  4096 5月  27 12:56 America/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Antarctica/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Arctic/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Asia/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Atlantic/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Australia/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Brazil/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Canada/
-rw-r--r--   1 root root  2102 4月  21 02:09 CET
drwxr-xr-x   2 root root  4096 5月  27 12:56 Chile/
-rw-r--r--   1 root root  2294 4月  21 02:09 CST6CDT
-rw-r--r--   1 root root  2437 4月  21 02:09 Cuba
-rw-r--r--   1 root root  1876 4月  21 02:09 EET
-rw-r--r--   1 root root  1972 4月  21 02:09 Egypt
-rw-r--r--   1 root root  3559 4月  21 02:09 Eire
-rw-r--r--   1 root root   127 4月  21 02:09 EST
-rw-r--r--   1 root root  2294 4月  21 02:09 EST5EDT
drwxr-xr-x   2 root root  4096 5月  27 12:56 Etc/
drwxr-xr-x   2 root root  4096 5月  27 12:56 Europe/
-rw-r--r--   1 root root   264 4月  21 02:09 Factory
-rw-r--r--   1 root root  3687 4月  21 02:09 GB
lrwxrwxrwx   1 root root     2 5月  27 09:07 GB-Eire -> GB
-rw-r--r--   1 root root   127 4月  21 02:09 GMT
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT0 -> GMT
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT-0 -> GMT
lrwxrwxrwx   1 root root     3 5月  27 09:07 GMT+0 -> GMT
lrwxrwxrwx   1 root root     3 5月  27 09:07 Greenwich -> GMT
-rw-r--r--   1 root root  1189 4月  21 02:09 Hongkong
-rw-r--r--   1 root root   128 4月  21 02:09 HST
-rw-r--r--   1 root root  1190 4月  21 02:09 Iceland
drwxr-xr-x   2 root root  4096 5月  27 12:56 Indian/
-rw-r--r--   1 root root  1678 4月  21 02:09 Iran
-rw-r--r--   1 root root  4475 2月  24 17:01 iso3166.tab
-rw-r--r--   1 root root  2265 4月  21 02:09 Israel
-rw-r--r--   1 root root   507 4月  21 02:09 Jamaica
-rw-r--r--   1 root root   355 4月  21 02:09 Japan
-rw-r--r--   1 root root   237 4月  21 02:09 Kwajalein
-rw-r--r--   1 root root 10384 2月  22 23:58 leap-seconds.list
-rw-r--r--   1 root root   655 4月  21 02:09 Libya
lrwxrwxrwx   1 root root    14 5月  27 09:07 localtime -> /etc/localtime
-rw-r--r--   1 root root  2102 4月  21 02:09 MET
drwxr-xr-x   2 root root  4096 5月  27 12:56 Mexico/
-rw-r--r--   1 root root   127 4月  21 02:09 MST
-rw-r--r--   1 root root  2294 4月  21 02:09 MST7MDT
-rw-r--r--   1 root root  2453 4月  21 02:09 Navajo
-rw-r--r--   1 root root  2460 4月  21 02:09 NZ
-rw-r--r--   1 root root  2057 4月  21 02:09 NZ-CHAT
drwxr-xr-x   2 root root  4096 5月  27 12:56 Pacific/
-rw-r--r--   1 root root  2705 4月  21 02:09 Poland
-rw-r--r--   1 root root  3453 4月  21 02:09 Portugal
drwxr-xr-x  19 root root  4096 5月  27 12:56 posix/
-rw-r--r--   1 root root  3545 4月  21 02:09 posixrules
-rw-r--r--   1 root root   414 4月  21 02:09 PRC
-rw-r--r--   1 root root  2294 4月  21 02:09 PST8PDT
drwxr-xr-x  19 root root  4096 5月  27 12:56 right/
-rw-r--r--   1 root root   800 4月  21 02:09 ROC
-rw-r--r--   1 root root   571 4月  21 02:09 ROK
-rw-r--r--   1 root root   428 4月  21 02:09 Singapore
drwxr-xr-x   2 root root  4096 5月  27 12:56 SystemV/
-rw-r--r--   1 root root  2747 4月  21 02:09 Turkey
-rw-r--r--   1 root root   127 4月  21 02:09 UCT
lrwxrwxrwx   1 root root     4 5月  27 09:07 Universal -> Zulu
drwxr-xr-x   2 root root  4096 5月  27 12:56 US/
lrwxrwxrwx   1 root root     4 5月  27 09:07 UTC -> Zulu
-rw-r--r--   1 root root  1873 4月  21 02:09 WET
-rw-r--r--   1 root root  1528 4月  21 02:09 W-SU
-rw-r--r--   1 root root 17533 4月  13 16:01 zone1970.tab
-rw-r--r--   1 root root 19014 4月  13 16:01 zone.tab
-rw-r--r--   1 root root   127 4月  21 02:09 Zulu

为解决此问题

可以选择东8区的Hongkong、Asia/Shanghai或者Asia/Hongkong作为参数,修改后url为

jdbc:mysql://172.xx.xx.xx:3306/test?useUnicode=true&characterEncoding=gbk&useSSL=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=Hongkong

时间返回就没问题了!

总结

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

相关文章

  • MySQL生成连续的数字/字符/时间序列的方法

    MySQL生成连续的数字/字符/时间序列的方法

    有时候为了生成测试数据,或者填充查询结果中的数据间隔,需要使用到一个连续的数据序列值,所以,今天我们就来介绍一下如何在 MySQL 中生成连续的数字、字符以及时间序列值,需要的朋友可以参考下
    2024-04-04
  • phpstudy安装后mysql无法启动的解决

    phpstudy安装后mysql无法启动的解决

    本文主要介绍了phpstudy安装后mysql无法启动的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Linux下编译安装Mysql 5.5的简单步骤

    Linux下编译安装Mysql 5.5的简单步骤

    Linux下面因为从MySQL 5.5开始使用cmake来做config了,所以编译安装的会和5.1版本有些区别。不过总体来说还是差别不大
    2015-08-08
  • MySQL8.0.21.0社区版安装教程(图文详解)

    MySQL8.0.21.0社区版安装教程(图文详解)

    这篇文章主要介绍了MySQL8.0.21.0社区版安装教程,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • mysql 数据类型TIMESTAMP

    mysql 数据类型TIMESTAMP

    timestamp数据类型是一个比较特殊的数据类型,他可以自动在你不使用程序更新情况下只要你更新了记录timestamp会自动更新时间
    2014-07-07
  • MySQL数据库事务原理及应用

    MySQL数据库事务原理及应用

    MySQL数据库事务是指一组数据库操作,要么全部执行成功,要么全部回滚。事务可以确保数据的一致性和完整性,避免了多个用户同时对同一数据进行修改所带来的问题。MySQL通过事务日志记录事务的操作,支持事务的回滚和提交等操作
    2023-04-04
  • MySQL字符集和排序规则详解(推荐)

    MySQL字符集和排序规则详解(推荐)

    MySQL中,字符集和排序规则在处理文本数据时起着至关重要的作用,本文将详细介绍 MySQL中的字符集和排序规则,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • MySQL整型数据溢出的解决方法

    MySQL整型数据溢出的解决方法

    这篇文章主要介绍了MySQL整型数据溢出的解决方法,本文出现整型溢出的mysql版本是5.1,5.1下整型溢出不会报错,而会变成负数,需要的朋友可以参考下
    2014-07-07
  • Mysql中的数据类型用法及解读

    Mysql中的数据类型用法及解读

    这篇文章主要介绍了Mysql中的数据类型用法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • Windows环境下MySQL 8.0 的安装、配置与卸载

    Windows环境下MySQL 8.0 的安装、配置与卸载

    这篇文章主要介绍了Windows环境下MySQL 8.0 的安装、配置与卸载步骤,本文分步骤给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09

最新评论