Java MySQL时间戳时区问题

我有一个java.util.Date对象,我需要以UTC格式将它插入MySQL的datetime字段。

 java.util.Date date = myDateFromSomewhereElse; PreparedStatement prep = con.prepareStatement( "INSERT INTO table (t1, t2) VALUES (?,?)"); java.sql.Timestamp t = new Timestamp(date.getTime()); prep.setTimestamp(1, t, Calendar.getInstance(TimeZone.getTimeZone("PST")); prep.setTimestamp(2, t, Calendar.getInstance(TimeZone.getTimeZone("UTC")); System.out.println(prep.toString()); 

这给了我准备好的SQL语句字符串:

 INSERT INTO table (t1, t2) VALUES ('2012-05-09 11:37:08','2012-05-09 11:37:08'); 

无论我指定的时区如何,返回的时间戳都是相同的时间戳。 它忽略了我指定的时区的Calendar对象。 发生了什么事,我做错了什么?

时区只是查看日期的不同方式(这是一个固定的时间点)。 我在这里写了一个小例子(密切关注断言):

 // timezone independent date (usually interpreted by the timezone of // the default locale of the user machine) Date now = new Date(); // now lets get explicit with how we wish to interpret the date Calendar london = Calendar.getInstance(TimeZone.getTimeZone("Europe/London")); Calendar paris = Calendar.getInstance(TimeZone.getTimeZone("Europe/Paris")); // now set the same date on two different calendar instance london.setTime(now); paris.setTime(now); // the time is the same assert london.getTimeInMillis() == paris.getTimeInMillis(); // London is interpreted one hour earlier than Paris (as of post date of 9th May 2012) String londonTime = london.get(Calendar.HOUR) + ":" + london.get(Calendar.MINUTE); String londonTZ = london.getTimeZone().getDisplayName(london.getTimeZone().inDaylightTime(london.getTime()), TimeZone.SHORT); System.out.println(londonTime + " " + londonTZ); // Paris is interpreted one hour later than Paris (as of post date of 9th May 2012) String parisTime = paris.get(Calendar.HOUR) + ":" + paris.get(Calendar.MINUTE); String parisTZ = paris.getTimeZone().getDisplayName(paris.getTimeZone().inDaylightTime(paris.getTime()), TimeZone.SHORT); System.out.println(parisTime + " " + parisTZ); 

此片段的输出是(结果将根据执行日期/时间而有所不同):

 8:18 BST 9:18 CEST 

问题中的代码片段根本没有对存储日期做任何事情。 通常,数据库是为本机TimeZone配置的。 我建议存储一个额外的字段,表示在解释日期时要使用的TimeZone。

修改日期(通常是固定时间点之前/之后的毫秒)通常不是(通常)一个好主意,因为这将是一个有损修改,在一年中的不同时间点会有不同的解释(由于夏令时)时间)。

或者: http : //puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/

乔丹,其实你有正确的想法。 问题是MySQL JDBC驱动程序中存在错误,默认情况下会完全忽略Calendar参数。 查看PreparedStatement的源代码,真正了解正在发生的事情。

请注意,它使用JVM的时区格式化时间戳。 这仅在您的JVM使用UTC时区时才有效。 Calendar对象完全被忽略。

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

为了让MySQL使用Calendar参数,您必须使用以下连接选项禁用旧版日期/时间代码:

 useLegacyDatetimeCode=false 

因此,您可以在连接到数据库时使用它,如下所示:

 String url = "jdbc:mysql://localhost/tz?useLegacyDatetimeCode=false" 

如果您使用上面的行禁用旧版日期时间代码,那么它将在目标日历的时区中呈现您的时间戳:

 if (targetCalendar != null) { targetCalendar.setTime(x); this.tsdf.setTimeZone(targetCalendar.getTimeZone()); timestampString = this.tsdf.format(x); } else { this.tsdf.setTimeZone(this.connection.getServerTimezoneTZ()); timestampString = this.tsdf.format(x); } 

很容易看到这里发生了什么。 如果传入Calendar对象,它将在格式化数据时使用它。 否则,它将使用数据库的时区来格式化数据。 奇怪的是,如果你传入一个日历,它也会将时间设置为给定的时间戳值(这似乎毫无意义)。

请查看此链接以获取有关MySQL的说明(您不应尝试将有关Oracle的建议应用于MySQL)。

TIMESTAMP数据类型用于包含日期和时间部分的值。 TIMESTAMP的范围为’1970-01-01 00:00:01’UTC到’2038-01-19 03:14:07’UTC。

MySQL将TIMESTAMP值从当前时区转换为UTC以进行存储,并从UTC转换回当前时区以进行检索。 (对于其他类型,例如DATETIME,不会发生这种情况。)默认情况下,每个连接的当前时区是服务器的时间。