Jor*_*dan 7 java mysql timezone timestamp
我有一个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());
Run Code Online (Sandbox Code Playgroud)
这给了我准备好的SQL语句字符串:
INSERT INTO table (t1, t2) VALUES ('2012-05-09 11:37:08','2012-05-09 11:37:08');
Run Code Online (Sandbox Code Playgroud)
无论我指定的时区如何,返回的时间戳都是相同的时间戳.它忽略了我指定的时区的Calendar对象.发生了什么事,我做错了什么?
乔丹,其实你有正确的想法.问题是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);
Run Code Online (Sandbox Code Playgroud)
为了让MySQL使用Calendar参数,您必须使用以下连接选项禁用旧版日期/时间代码:
useLegacyDatetimeCode=false
Run Code Online (Sandbox Code Playgroud)
因此,您可以在连接到数据库时使用它,如下所示:
String url = "jdbc:mysql://localhost/tz?useLegacyDatetimeCode=false"
Run Code Online (Sandbox Code Playgroud)
如果您使用上面的行禁用旧版日期时间代码,那么它将在目标日历的时区中呈现您的时间戳:
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);
}
Run Code Online (Sandbox Code Playgroud)
很容易看出这里发生了什么.如果传入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);
Run Code Online (Sandbox Code Playgroud)
此代码片段的输出为(结果将根据执行日期/时间而有所不同):
8:18 BST
9:18 CEST
Run Code Online (Sandbox Code Playgroud)
您在问题中的代码片段根本没有对存储的日期执行任何操作。通常数据库是针对本机时区配置的。我建议存储一个表示解释日期时使用的时区的额外字段。
修改日期(通常是固定时间点之前/之后的毫秒)并不是一个好主意,因为这将是一种有损修改,在一年中的不同时间点会有不同的解释(由于夏令时)时间)。
或者这个: http: //puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/