public Timestamp getCurrentUTC(LocalDateTime date, DateTimeZone srcTZ, DateTimeZone dstTZ, Locale l) {
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
System.out.println("UTC Time:" + dstDateTime.getMillis());
System.out.println("UTC Time:" + new Timestamp(dstDateTime.getMillis()));
return new Timestamp(dstDateTime.getMillis());
}
程序的输出如下所示:
UTC Time:1378265162047
UTC Time:2013-09-03 23:26:02.047
毫秒值是正确的UTC时间(即与GMT-4时区确认)。 秒值是EST时区。
我需要的是不改变UTC值的java.sql.Timestamp(即与时区无关的)用于数据库写入。是否可能?
Edit 1
DateTime srcDateTime = date.toDateTime(srcTZ);
DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);
System.out.println("UTC Time:" + dstDateTime.getMillis());
我知道srcDateTime
是本地时间(GMT-4),dstDateTime
是世界标准时间(GMT-0)。日期的输出值如下:
Source Date:2013-09-04T09:10:43.683-04:00
Destination Date: 2013-09-04T13:10:43.683Z
我尝试了所有的组合来获取dstDateTime
的UTC值作为java.sql.TimeStamp:
System.out.println("UTC Time:" + dstDateTime.getMillis());
System.out.println("UTC Time:" + new Timestamp(srcDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
System.out.println("UTC Time:" + new Timestamp(dstDateTime.toDateTime(DateTimeZone.UTC).getMillis()));
测试的打印输出:
UTC Time:1378298760226 - Correct UTC
UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
UTC Time:2013-09-04 08:46:00.226 - Incorrect Local Date Time instead of the Expected UTC
第一行打印正确的UTC时间戳。我所需要的就是相同类型的java.sql.TimeStamp值。无论我尝试什么,总是返回本地机器的日期时间。
编辑2
我尝试了以下方法:
System.out.println("UTC Timestamp:" + date.toDateTime(srcTZ).getMillis());
System.out.println("UTC Timestamp:" + new Timestamp(date.toDateTime(srcTZ).getMillis()));
输出结果如下:
UTC Time:1378342856315 - Correct UTC Time
UTC Timestap:2013-09-04 21:00:56.315 - Local Time other than the expected UTC Time
每当我尝试转换为时间戳时,我会失去我想要的有效UTC值。
就方法参数而言:
srcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Montreal")
dstTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Etc/UTC"))
Local l = new Locale("en", "CA")
非常感谢你的帮助。
Nick。
第三次编辑
你好,Matt,
非常感谢您的回复。我们得到了与您相同的结果。不知道关于打印等的事情...更具体地说:
System.out.println("UTC Timestamp:" + srcDateTime.toDateTime(dstTZ).getMillis());
System.out.println("UTC Timestamp:" + srcDateTime.toDateTime(dstTZ));
System.out.println("UTC Timestamp:" + new Timestamp(srcDateTime.toDateTime(dstTZ).getMillis()));
输出结果如下:
UTC Timestamp:1378389098468 - Correct UTC Timestap (Thu, 05 Sep 2013 13:51:38 GMT)
UTC Timestamp:2013-09-05T13:51:38.468Z - Correct UTC Time
UTC Timestamp:2013-09-05 09:51:38.468 - Local time is printed, UTC is expected
当我们意识到数据库存储的是本地时间而非UTC时间时,这个问题被带到了我的视线中。
+---------------------+
| effectivedate |
+---------------------+
| 2013-09-05 09:34:11 |
+---------------------+
MySQL时区设置为'-00:00'
mysql> SELECT CURRENT_TIMESTAMP;
+---------------------+
| CURRENT_TIMESTAMP |
+---------------------+
| 2013-09-05 13:48:09 |
+---------------------+
使用Eclipse调试器调试应用程序时,我们发现本地日期时间(2013-09-05 09:51:38.468)被传递到了数据库中(无法发布图片,积分不足...)。数据类型为TimeStamp,没有字符串操作。也许Eclipse调试器同样使用了
String.println()
函数,但不确定...非常感谢您帮助我们调试应用程序。不想浪费太多时间和精力(开个小玩笑)...顺祝商祺,Nick.