【问题标题】:Java MySQL Timestamp time zone problemsJava MySQL Timestamp 时区问题
【发布时间】:2012-05-18 07:59:30
【问题描述】:

我有一个java.util.Date 对象,我需要将它以UTC 格式插入到MySQL 的日期时间字段中。

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 对象。发生了什么事,我做错了什么?

【问题讨论】:

  • 时区不会更改时间戳。它们只是更改日期呈现时显示的内容。
  • 是的,我知道时间戳只是自 GMT 纪元以来的毫秒数,与时区无关。我的问题是,即使指定了时区,它也会呈现完全相同的显示日期(请注意,即使我指定了不同的时区,日期在最终 SQL 命令中也呈现完全相同)。
  • 我想这可能就是你要找的东西:puretech.paawak.com/2010/11/02/…

标签: java mysql timezone timestamp


【解决方案1】:

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

// 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);

这个sn-p的输出是(结果会根据执行日期/时间而不同):

8:18 BST
9:18 CEST

您在问题中的 sn-p 根本没有对存储的日期做任何事情。通常数据库配置为本地时区。我建议存储一个额外的字段来表示解释日期时要使用的时区。

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

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

【讨论】:

  • 实际上他可以在数据库中接收不同的值,如果他的数据库是Oracle并且他使用列类型TIMESTAMP WITH TIME ZONE。但是 MySQL 中的日期没有时区支持。
  • 是的,我在底部放置了一个链接,用于存储/检索时区以及日期的 Oracle 特定实现。我认为单独存储时区更通用,因为它使非 Oracle 数据库更便于移植。即使没有 TimeZone 上下文,日期的优点在于可以毫无问题地相互比较。
  • 如果您确实需要保留时区信息,最好使用数据库特定功能(如 Oracle 和 PostgreSQL 中的)而忘记可移植性。否则你会遇到比较和排序的问题。
  • @VadimPonomarev:如果不使用数据库特定的时区功能,那么您在排序时遇到问题是错误的。日期/时间存储为固定时间点整数(表示固定时间点之前或之后的时间)。因此,它们始终可以完美地排序和比较,而无需了解它们所代表的时区。
  • 是的,你让我来了。没有想过这种可能性。 +1 评论。
【解决方案2】:

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

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

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

【讨论】:

    【解决方案3】:

    乔丹,其实你的想法是对的。问题是 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 对象,它将在格式化数据时使用它。否则,它将使用数据库的时区来格式化数据。奇怪的是,如果你传入一个 Calendar,它也会将时间设置为给定的 Timestamp 值(这似乎没有意义)。

    【讨论】: