UTC时间戳+ Joda时间 [英] UTC Timestamp + Joda Time

查看:107
本文介绍了UTC时间戳+ Joda时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to get the UTC TimeStamp in a simple Java program using Joda:

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

The millisecond value is the correct UTC time (i.e. confirmed with GMT-4 timezone) The second value is the EST timezone.

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

What I need is the UTC value unchanged as java.sql.Timestamp (ie TZ independent), for a database write. Is this possible?

DateTime srcDateTime = date.toDateTime(srcTZ);

DateTime dstDateTime = srcDateTime.toDateTime(dstTZ);

System.out.println("UTC Time:" + dstDateTime.getMillis());

我知道 srcDateTime 是本地日期(GMT-4)和 dstDateTime
UTC(GMT-0)。日期的输出值如下:

I know that srcDateTime is the local date (GMT-4), and dstDateTime is UTC (GMT-0). Output values of the dates are as follows:

Source Date:2013-09-04T09:10:43.683-04:00

Destination Date: 2013-09-04T13:10:43.683Z

我尝试了所有组合,尝试将 dstDateTime 的UTC值作为java.sql.TimeStamp:

I tried all the combinations to try to get the UTC value of dstDateTime as a 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类型相同的值
。我试过的任何东西总是返回机器的本地日期时间

The first print line is the correct UTC timestamp. All I need is that same value as type java.sql.TimeStamp. Anything I tried always returned the local date time of the machine.

我尝试了以下内容:

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值。

Whenever I try to convert to TimeStamp, I loose the valid UTC value that I am after.

就方法的参数而言:

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

非常感谢任何帮助。

Nick 。

Hello Matt,

Hello Matt,

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

Thank you so much for your response. We are getting the same results as you. Did not know the thing about printing etc.. More specifically:

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时,问题引起了我的注意:

The problem was brought to my attention when we realized that the DB was storing the local time instead of UTC:

+---------------------+
| effectivedate       |
+---------------------+
| 2013-09-05 09:34:11 |
+---------------------+

Mysql时区设置为'-00:00'

The Mysql timezone is set to '-00:00'

mysql> SELECT CURRENT_TIMESTAMP;
+---------------------+
| CURRENT_TIMESTAMP   |
+---------------------+
| 2013-09-05 13:48:09 |
+---------------------+

使用eclipse调试器调试应用程序我们意识到本地日期时间(2013-09-05 09:51:38.468)正在传递给DB(无法发布图像,没有足够的点... )。数据类型是直接的TimeStamp,没有字符串操作。也许eclipse调试器也使用 String.println()函数,不确定..

Debugging the application using eclipse debugger we realized that the local date time (2013-09-05 09:51:38.468) was being passed to the DB (Can't post images, not enough points...). The datatype is straight TimeStamp, with no string manipulation. Maybe the eclipse debugger is using String.println() function as well, not sure..

我真的很感激所有帮助调试我们的应用程序。不想占用这么多时间(没有双关语意)和努力......

I really appreciate all the help debugging our application. Did not want to take up so much time (no pun intended) and effort...

亲切的问候,

尼克。

推荐答案

我希望这可以节省3天的废话。在代码中的某个逻辑位置设置默认时区。使事情变得更加便携,必须设置一个env变量等。你可以通过在你的代码,构造函数等中添加以下逻辑来实现这一点。:

I hope this saves someone 3 days of bullshit. Set the default timezone somewhere logical in your code. Makes things more portable that having to set an env variable etc.. You can do this by adding the following somewhere logical to your code, Constructor etc..:

DateTimeZone。 setDefault(DateTimeZone.UTC);

DateTimeZone.setDefault(DateTimeZone.UTC);

你可以打印UTC,连接UTC无论......

You can print UTC, concatenate UTC whatever...

这篇关于UTC时间戳+ Joda时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆