CST时区名为CST的时区是一个很混乱的时区,有四种含义:

-美国中部时间CentralStandardTime(USA)UTC-06:00

-澳大利亚中部时间CentralStandardTime(Australia)UTC+09:30

-中国标准时ChinaStandardTimeUTC+08:00

-古巴标准时CubaStandar

可能存在mysql读取TimeStamp字段时区和java时区相差13小时的现象

其原因为:

public void configureTimezone() {

String configuredTimeZoneOnServer = getServerVariable("time_zone");

if ("SYSTEM".equalsIgnoreCase(configuredTimeZoneOnServer)) {

configuredTimeZoneOnServer = getServerVariable("system_time_zone");

}

String canonicalTimezone = getPropertySet().getStringReadableProperty(PropertyDefinitions.PNAME_serverTimezone).getValue();

if (configuredTimeZoneOnServer != null) {

// user can override this with driver properties, so don't detect if that's the case

if (canonicalTimezone == null || StringUtils.isEmptyOrWhitespaceOnly(canonicalTimezone)) {

try {

canonicalTimezone = TimeUtil.getCanonicalTimezone(configuredTimeZoneOnServer, getExceptionInterceptor());

} catch (IllegalArgumentException iae) {

throw ExceptionFactory.createException(WrongArgumentException.class, iae.getMessage(), getExceptionInterceptor());

}

}

}

if (canonicalTimezone != null &;&; canonicalTimezone.length() > 0) {

this.serverTimezoneTZ = TimeZone.getTimeZone(canonicalTimezone);

// The Calendar class has the behavior of mapping unknown timezones to 'GMT' instead of throwing an exception, so we must check for this...

if (!canonicalTimezone.equalsIgnoreCase("GMT")

&;&; this.serverTimezoneTZ.getID().equals("GMT")) {

throw ...

}

}

this.defaultTimeZone = this.serverTimezoneTZ;

}

mysql> show variables like '%time_zone%';

+------------------+--------+

| Variable_name| Value|

+------------------+--------+

| system_time_zone | CST|

| time_zone | SYSTEM |

+------------------+--------+

当 JDBC 与 MySQL 开始建立连接时,会调用 `com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer()` 获取服务器参数

代码中可见当 MySQL 的 `time_zone` 值为 `SYSTEM` 时,会取 `system_time_zone` 值作为协调时区。

若 `String configuredTimeZoneOnServer` 得到的是 `CST` 那么 Java 会误以为这是 `CST -0500`,

当服务器的时区为`CST +0800`时误认为java误认为时区为 `CST -0500`,导致java获取结果相差13小时。

`com.mysql.cj.jdbc.PreparedStatement.setTimestamp()`。

```java

public void setTimestamp(int parameterIndex, Timestamp x) throws java.sql.SQLException {

synchronized (checkClosed().getConnectionMutex()) {

setTimestampInternal(parameterIndex, x,this.session.getDefaultTimeZone());

}

}

这里的this.session.getDefaultTimeZone()是java默认得到的`CST -0500`,

private void setTimestampInternal(int parameterIndex, Timestamp x, TimeZone tz) throws SQLException {

if (x == null) {

setNull(parameterIndex, MysqlType.TIMESTAMP);

} else {

if (!this.sendFractionalSeconds.getValue()) {

x = TimeUtil.truncateFractionalSeconds(x);

}

this.parameterTypes[parameterIndex - 1 + getParameterIndexOffset()] = MysqlType.TIMESTAMP;

if (this.tsdf == null) {

this.tsdf = new SimpleDateFormat("''yyyy-MM-dd HH:mm:ss", Locale.US);

}

this.tsdf.setTimeZone(tz);

StringBuffer buf = new StringBuffer();

buf.append(this.tsdf.format(x));

if (this.session.serverSupportsFracSecs()) {

buf.append('.');

buf.append(TimeUtil.formatNanos(x.getNanos(), true));

}

buf.append('\'');

setInternal(parameterIndex, buf.toString());

}

}

此处 Timestamp 被转换为会话时区的时间字符串了

在此时jdbc误认为会话时区在 CST-5

JBDC 把 Timestamp+0 转为 CST-5 的 String-5

MySQL 认为会话时区在 CST+8,将 String-5 转为 Timestamp-13

最终结果相差 13 个小时!如果处在冬令时还会相差 14 个小时!

解决办法也很简单,明确指定 MySQL 数据库的时区,不使用引发误解的 `CST`:

mysql> set global time_zone = '+08:00';

Query OK, 0 rows affected (0.00 sec)

mysql> set time_zone = '+08:00';

Query OK, 0 rows affected (0.00 sec)

或者修改 `my.cnf` 文件,在 `[mysqld]` 节下增加 `default-time-zone = '+08:00'`。

修改时区操作影响深远,需要重启 MySQL 服务器,建议在维护时间进行。

dirver时区_时区问题相关推荐

  1. mysql内部时区_一文解决MySQL时区相关问题

    前言: 在使用MySQL的过程中,你可能会遇到时区相关问题,比如说时间显示错误.时区不是东八区.程序取得的时间和数据库存储的时间不一致等等问题.其实,这些问题都与数据库时区设置有关,本篇文章将从数据库 ...

  2. 东八区转为0时区_什么叫0时区

    展开全部 零时区: 7.5°W-7.5°E 时区中心线 0° .它只是一个时区,就像北京的东8区.在这个32313133353236313431303231363533e58685e5aeb93133 ...

  3. mysql timestamp 当前_时区信息记录表|全方位认识 mysql 系统库

    作者  罗小波 · 沃趣科技高级数据库技术专家 出品  沃趣科技 在上一期<优化器成本记录表|全方位认识 mysql 系统库>中,我们详细介绍了mysql 系统库中的优化器成本记录表,本期 ...

  4. JS一个根据时区输出时区时间的函数

    做项目遇到的坑爹问题,需要根据时区获取时区中轴线的时间.为此搜了好久网上都没什么JS的代码描述到这一方面,最后自己翻了下高中地理才写了个函数出来. 此图可以看出来,全球分为了0时区,东西1-11区,第 ...

  5. mysql查看时区 go时区问题

    mysql 时区问题 1 查看mysql时区的三个命令 show variables like'%time_zone';select NOW();select @@global.time_zone,@ ...

  6. dirver时区_【作业】偶入卡地亚Driver de Cartier双时区大日历腕表|腕表之家xbiao.com...

    国庆期间去泰国玩,最后一站到了曼谷的王权免税店.上一块表还是2012年的时候去香港买的无日历黑水鬼.戴了4年了,非常喜欢.但一直总想再入一款皮带表,于是相中了心仪很久的万国7日链,无奈免税店里最后一块 ...

  7. mysql6.0设置时区_关于Mysql6.0+的时区错乱问题

    如果使用mysql6.0+的JDBC驱动版本的时候,有时候会出现程序时间与数据库时间相差很多个小时; 1.如果以北京时间为例,相差8个小时的情况一般是你在连接jdbc的url中没有标明system_t ...

  8. mysql中如何设置时区_如何设置MySQL的时区?

    我认为这可能是有用的: 有三个位置可以在MySQL中设置时区: 在[mysqld]部分中的"my.cnf"文件中default-time-zone='+00:00' @global ...

  9. linux 修改时区_如何在 Ubuntu 20.04 上设置或者修改时区

    本文最先发布在: 如何在 Ubuntu 20.04 上设置或者修改时区​www.itcoder.tech 使用正确的时区,对于系统相关的任务和进程来说,是最基本的.例如,cron 守护进程,使用系统时 ...

  10. php 设置时区_为什么没有 Asia/Beijing 时区?

    在设置系统时间.开发PHP和Java程序(例如用JDBC访问MySQL数据库)的时候,有时需要选择本地时区.通常,时区列表中和中国有关的共有六个本地时区:Asia/Chongqing.Asia/Sha ...

最新文章

  1. Python fabric实现远程操作和部署
  2. 从 Vuex 0.6.x 迁移到 1.0
  3. spark sql基本使用方法介绍(转载)
  4. [20161006]windows下bbed使用注意.txt
  5. 机器学习中的alpha学习率参数
  6. php yii2 api框架,Yii2框架制作RESTful风格的API快速入门教程
  7. 指针用完了怎么办_墙体里水管漏水怎么办?瞧瞧这个,轻松搞定,滴水不漏
  8. office365安装文件_Microsoft 365(原Office 365)离线下载安装教程
  9. 公司内网机器vm ubuntu proxy 设置
  10. HP DV3 笔记本 重装系统
  11. 自动驾驶_感知_跟踪
  12. jboss下载(jboss安装与配置)
  13. 计算机配置里面没有网络选项,win7网络共享中心里面的更改适配器设置里面没有无线网络连接...-win7适配器没有无线网络连接,win7网络适配器未...
  14. 万年历,阴阳历转换。
  15. 闲鱼:Thread.sleep(0) 到底有什么用?我:有点懵~
  16. vue中a的href写法
  17. 学习笔记:清华慕课——数据结构(1)
  18. 软件工程结课论文 敏捷开发在软件工程中的应用 大学编程作业(TUST 天津科技大学 2022年)
  19. 门头沟的《一出好戏》
  20. c语言中的二目运算符,C语言中的三目运算符是什么

热门文章

  1. 电脑连无线无法访问云服务器,小编教你如何解决电脑无法连接无线网络
  2. 点点动画~画出懂你的3D魔方
  3. c语言用switch编写春夏秋冬,利用switch实现春夏秋冬
  4. 【leetcode】1419. Minimum Number of Frogs Croaking
  5. matlab tdb,计算相图中的TDB文件 - 计算模拟 - 小木虫 - 学术 科研 互动社区
  6. 该内存不能为 read/written解决办法
  7. 国际贸易相关术语-DDP
  8. php 转义反绡线,PHP汉字转拼音
  9. 六年工作经验总结分享,希望可以帮到你
  10. 计算机笔记Excel,秦路天善智能EXCEL学习笔记1-文本清洗函数