意外的MySQL DATETIME行为

时间:2015-05-28 19:21:48

标签: java mysql datetime

当我尝试从我的Java应用程序向我的表中插入值时,我看到了MySQL DATETIME中出现的意外行为。我已经阅读了很多有关DATETIME的StackOverflow问题和答案,但这似乎是一个完全不同的问题。

我的Java应用程序使用MyBatis和this DateTimeTypeHandler插入joda DateTime值2015-03-04T22:30:59.922Z。 (我还修改了上面的代码以使用setTimestamp(int,Timestamp,Calendar)方法,但没有注意到结果的差异)。

我看到插入表中的是值2015-03-04 14:30:59.922。我读到MySQL DATETIME使用服务器默认时区。我住在PDT时区,所以我希望价值为2015-03-04 15:30:59.922(15:30而不是14:30)。

令人惊讶的是,MySQL似乎并没有表明PDT的时区。那么为什么表中的日期时间值是2015-03-04 14:30:59.922?

mysql> select @@global.time_zone; +--------------------+ | @@global.time_zone | +--------------------+ | UTC | +--------------------+ 1 row in set (0.06 sec)

mysql> select @@session.time_zone; +---------------------+ | @@session.time_zone | +---------------------+ | UTC | +---------------------+ 1 row in set (0.07 sec)

mysql> SELECT @@system_time_zone; +--------------------+ | @@system_time_zone | +--------------------+ | UTC | +--------------------+ 1 row in set (0.07 sec)

1 个答案:

答案 0 :(得分:1)

2015年3月4日为标准时间,因此偏移为-8小时:

UTC (Time Zone)                   Wednesday, March 4, 2015 at 10:30:00 PM     UTC         
Los Angeles (U.S.A. - California) Wednesday, March 4, 2015 at 2:30:00 PM  PST UTC-8 hours 

今天(2015年5月28日)是夏令时,因此偏移为-7小时:

UTC (Time Zone)                   Thursday, May 28, 2015 at 10:30:00 PM     UTC         
Los Angeles (U.S.A. - California) Thursday, May 28, 2015 at 3:30:00 PM  PDT UTC-7 hours