【发布时间】:2025-02-21 13:05:01
【问题描述】:
我有一个名为 startDate 的字段,类型为 TIMESTAMP,我从 MySQL InnoDB 数据库获得:
SELECT startDate FROM jn.all WHERE id IN(115)
=> 2012-07-28 00:00:00
在 Java 中我会这样做:
java.util.Date d = resultSet.getDate("startDate");
SimpleDateFormat tsmpFormatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
tsmpFormatter.setTimeZone(TimeZone.getTimeZone("UTC"));
System.out.println(tsmpFormatter.format(d));
我明白了:
=>2012-07-28 04:00:00
从 UTC 到美国东部标准时间有 4 小时。数据库时间为 UTC。
SELECT now();
=> 2013-07-29 21:46:26
而我目前的 EST 时间是 17:46:26
为什么即使我在所有地方都使用 UTC,也会有 4 小时的差异?谢谢!
编辑:
我可能已经找到问题了。
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.)
但这是什么current time zone?服务器应该是 UTC。
【问题讨论】:
-
你知道为什么当前时区是 EST 而不是 UTC 吗?