JodaTime - 如何获得UTC的当前时间 [英] JodaTime - how to get current time in UTC

查看:459
本文介绍了JodaTime - 如何获得UTC的当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以UTC格式获取当前时间。到目前为止我所做的是(仅用于测试目的):

  DateTime dt = new DateTime(); 
DateTimeZone tz = DateTimeZone.getDefault();
LocalDateTime nowLocal = new LocalDateTime();
DateTime nowUTC = nowLocal.toDateTime(DateTimeZone.UTC);

日期d1 = nowLocal.toDate();
日期d2 = nowUTC.toDate();

L.d(tz:+ tz.toString());
L.d(local:+ d1.toString());
L.d(utc:+ d2.toString());




  • d1 是我当地时间,没关系

  • d2 是我当地时间+ 1,但应该是当地时间 - 1 ...



我的本​​地时区是UTC + 1(根据调试输出和此处的列表: http://joda-time.sourceforge.net/timezones.html )...



如何正确地从一个时区转换为另一个时区(以毫秒为单位)?



编辑



我需要日期/毫秒...这不是关于正确显示时间....



编辑2



现在,在评论和答案的帮助下,我尝试了以下内容:

  DateTimeZone tz = DateTimeZone.getDefault(); 
DateTime nowLocal = new DateTime();
LocalDateTime nowUTC = nowLocal.withZone(DateTimeZone.UTC).toLocalDateTime();
DateTime nowUTC2 = nowLocal.withZone(DateTimeZone.UTC);

日期dLocal = nowLocal.toDate();
日期dUTC = nowUTC.toDate();
日期dUTC2 = nowUTC2.toDate();

L.d(Temp.class,------------------------);
L.d(Temp.class,tz:+ tz.toString());
L.d(Temp.class,local:+ nowLocal +|+ dLocal.toString());
L.d(Temp.class,utc:+ nowUTC +|+ dUTC.toString()); //< = WORKING SOLUTION
L.d(Temp.class,utc2:+ nowUTC2 +|+ dUTC2.toString());

OUTPUT

  tz:Europe / Belgrade 
local:2015-01-02T15:31:38.241 + 01:00 | 1月2日星期五15:31:38 MEZ 2015
utc:2015-01-02T14:31:38.241 | 1月2日星期四14:31:38 MEZ 2015
utc2:2015-01-02T14:31:38.241Z | 1月2日星期五15:31:38 MEZ 2015

我想要的是,当地日期显示15时钟和utc日期显示14点...
现在,这似乎有效...



---- - EDIT3 - 最终解决方案-----



希望这是一个很好的解决方案......我想,我尊重所有的tipps ... 。

  DateTimeZone tz = DateTimeZone.getDefault(); 
DateTime nowUTC = new DateTime(DateTimeZone.UTC);
DateTime nowLocal = nowUTC.withZone(tz);

//这将产生不同的日期!!!我想要它!
日期dLocal = nowLocal.toLocalDateTime()。toDate();
日期dUTC = nowUTC.toLocalDateTime()。toDate();

L.d(tz:+ tz.toString());
L.d(local:+ nowLocal +|+ dLocal.toString());
L.d(utc:+ nowUTC +|+ dUTC.toString());

输出:

  tz:Europe / Belgrade 
local:2015-01-03T21:15:35.170 + 01:00 | 1月3日星期六21:15:35 MEZ 2015
utc:2015-01-03T20:15:35.170Z | 1月3日星期六20:15:35 MEZ 2015


解决方案

你让它变得比你需要的复杂得多:

  DateTime dt = new DateTime(DateTimeZone.UTC); 

根本不需要转换。如果您发现实际需要转换,可以使用 withZone 。我建议你避免通过 LocalDateTime ,但是,由于时区转换,你可能会丢失信息(两个不同的时刻可能会有在同一时区内的当地时间相同,因为时钟会返回并重复当地时间。



说完所有这些后,为了可测试性,我个人喜欢使用时钟界面,它允许我获取当前时间(例如,作为 Instant )。然后您可以使用依赖注入在生产中运行时注入一个真实的系统时钟,以及一个预设时间用于测试的假时钟.Java 8的 java.time 包内置了这个想法,顺便说一下。 / p>

I want to get the current time in UTC. What I do so far is following (just for testing purposes):

    DateTime dt = new DateTime();
    DateTimeZone tz = DateTimeZone.getDefault();
    LocalDateTime nowLocal = new LocalDateTime();
    DateTime nowUTC = nowLocal.toDateTime(DateTimeZone.UTC);

    Date d1 = nowLocal.toDate();
    Date d2 = nowUTC.toDate();

    L.d("tz: " + tz.toString());
    L.d("local: " + d1.toString());
    L.d("utc: " + d2.toString());

  • d1 is my local time, that's fine
  • d2 is my local time + 1, but should be local time - 1...

My local time zone is UTC+1 (according to the debug output and the list here: http://joda-time.sourceforge.net/timezones.html)...

How do I correctly convert from one time zone to another (inlcusive the millisecond representation)?

EDIT

I need the date/milliseconds... It's NOT about displaying the time correctly....

EDIT 2

Now, with the help of a comment and an answer, I tried following:

    DateTimeZone tz = DateTimeZone.getDefault();
    DateTime nowLocal = new DateTime();
    LocalDateTime nowUTC = nowLocal.withZone(DateTimeZone.UTC).toLocalDateTime();
    DateTime nowUTC2 = nowLocal.withZone(DateTimeZone.UTC);

    Date dLocal = nowLocal.toDate();
    Date dUTC = nowUTC.toDate();
    Date dUTC2 = nowUTC2.toDate();

    L.d(Temp.class, "------------------------");
    L.d(Temp.class, "tz    : " + tz.toString());
    L.d(Temp.class, "local : " + nowLocal +     " | " + dLocal.toString());
    L.d(Temp.class, "utc   : " + nowUTC +       " | " + dUTC.toString()); // <= WORKING SOLUTION
    L.d(Temp.class, "utc2  : " + nowUTC2 +      " | " + dUTC2.toString());

OUTPUT

tz    : Europe/Belgrade
local : 2015-01-02T15:31:38.241+01:00 | Fri Jan 02 15:31:38 MEZ 2015
utc   : 2015-01-02T14:31:38.241 | Fri Jan 02 14:31:38 MEZ 2015
utc2  : 2015-01-02T14:31:38.241Z | Fri Jan 02 15:31:38 MEZ 2015

What I wanted was, that the local date displays 15 o'clock and utc date displays 14 o'clock... For now, this seems to work...

----- EDIT3 - Final solution -----

Hopefully, this is a good solution... I think, i respects all tipps i got...

    DateTimeZone tz = DateTimeZone.getDefault();
    DateTime nowUTC = new DateTime(DateTimeZone.UTC);
    DateTime nowLocal = nowUTC.withZone(tz);

    // This will generate DIFFERENT Dates!!! As I want it!
    Date dLocal = nowLocal.toLocalDateTime().toDate();
    Date dUTC = nowUTC.toLocalDateTime().toDate();

    L.d("tz    : " + tz.toString());
    L.d("local : " + nowLocal +     " | " + dLocal.toString());
    L.d("utc   : " + nowUTC +       " | " + dUTC.toString());

Output:

tz    : Europe/Belgrade
local : 2015-01-03T21:15:35.170+01:00 | Sat Jan 03 21:15:35 MEZ 2015
utc   : 2015-01-03T20:15:35.170Z | Sat Jan 03 20:15:35 MEZ 2015

解决方案

You're making it far more complicated than you need to:

DateTime dt = new DateTime(DateTimeZone.UTC);

No conversion required at all. If you find you actually need to convert, you can use withZone. I'd suggest you avoid going via LocalDateTime, however, as that way you can lose information due to time zone transitions (two different instants can have the same local time in the same time zone, because clocks go back and repeat local time.

Having said all of this, for the sake of testability I personally like using a Clock interface which allows me to get the current time (e.g. as an Instant). You can then use dependency injection to inject a real system clock when running in production, and a fake clock with a preset time for tests. Java 8's java.time package has this idea built into it, btw.

这篇关于JodaTime - 如何获得UTC的当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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