java - UTC 时间戳 + Joda 时间

标签 java jodatime

我正在尝试使用 Joda 在一个简单的 Java 程序中获取 UTC 时间戳:

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 时区。

我需要的是未更改为 java.sql.Timestamp 的 UTC 值(即独立于 TZ), 用于数据库写入。这可能吗?

编辑 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 时间戳。我所需要的只是相同的值(value) 作为 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

每当我尝试转换为 TimeStamp 时,我都会丢失我想要的有效 UTC 值。

在方法的参数方面:

srcTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/Montreal")
dstTZ = DateTimeZone.forTimeZone(TimeZone.getTimeZone("Etc/UTC"))
Local l = new Locale("en", "CA")

非常感谢任何帮助。

尼克。

编辑3

你好马特,

非常感谢您的回复。我们得到的结果与您相同。不知道有关打印等的事情。更具体地说:

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) 正在传递给数据库(无法发布图像,积分不足...)。数据类型是直接时间戳,没有字符串操作。也许 eclipse 调试器也在使用 String.println() 函数,不确定..

我非常感谢所有帮助调试我们的应用程序的人。不想占用太多时间(没有双关语意)和精力...

亲切的问候,

尼克。

最佳答案

我希望这可以为某人节省 3 天的废话。在代码中的某个逻辑位置设置默认时区。使必须设置 env 变量等的东西更便携。您可以通过在代码、构造函数等逻辑位置添加以下内容来做到这一点:

DateTimeZone.setDefault(DateTimeZone.UTC);

您可以打印 UTC,连接 UTC 任何...

关于java - UTC 时间戳 + Joda 时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18604989/

相关文章:

java - 此 C OpenSSL 加密函数的 Java JCE 等价物是什么?

java - Java 中的 SolidWorks 亿图查看器

java - 相当于 Joda 时间的 DAY_OF_WEEK_IN_MONTH

java - Joda LocalTime Meridiem 解析

java - Joda的DateTime与java.util.Calendar不一致

java - 如何从静态方法引用非静态变量?

java - org.dbunit.database - junit.framework.ComparisonFailure - 表顺序每次运行都不同

java - 当我删除字段时,Firestore EventListener 导致 Null 异常

jodatime - 使用 Joda-Time 获取时区本地毫秒数的最简单方法

java - 为平台中立的消费保留时间戳的首选方式