简体   繁体   中英

Unexpected MySQL DATETIME behavior

I'm seeing what seems to me unexpected behavior from MySQL DATETIME when I'm trying to insert values into my table from my Java application. I've read a lot of StackOverflow questions and answers regarding DATETIME, but this seems to be a totally different issue.

My Java application uses MyBatis and this DateTimeTypeHandler to insert a joda DateTime value 2015-03-04T22:30:59.922Z . (I also modified the above code to use the setTimestamp(int, Timestamp, Calendar) method but did not notice and difference in results).

What I see being inserted into the table is the value 2015-03-04 14:30:59.922 . I read that MySQL DATETIME uses the server default timezone. I live in the PDT timezone so I would expect the value to be 2015-03-04 15:30:59.922 (that's 15:30 instead of 14:30).

What's surprising is nothing in MySQL seems to indicate the PDT timezone. So why would my datetime value in the table be 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)

March 4, 2015 was Standard time so the offset is -8 hours:

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 

Today (May 28, 2015) is Daylight time so the offset is -7 hours:

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM