Joda Time - different between timezones

2019-01-19 00:01发布

I want to convert the current time to the time in a specific timezone with Joda time.

Is there a way to convert DateTime time = new DateTime() to a specific timezone, or perhaps to get the number of hours difference between time.getZone() and another DateTimeZone to then do time.minusHours or time.plusHours?

2条回答
淡お忘
2楼-- · 2019-01-19 00:12

I want to convert the current time to the time in a specific timezone with Joda time.

It's not really clear whether you've already got the current time or not. If you've already got it, you can use withZone:

DateTime zoned = original.withZone(zone);

If you're just fetching the current time, use the appropriate constructor:

DateTime zoned = new DateTime(zone);

or use DateTime.now:

DateTime zoned = DateTime.now(zone);
查看更多
叛逆
3楼-- · 2019-01-19 00:19

check out DateTimeZone & Interval at
http://joda-time.sourceforge.net/apidocs/org/joda/time/DateTimeZone.html & http://joda-time.sourceforge.net/apidocs/org/joda/time/Interval.html

DateTime dt = new DateTime();
    // translate to London local time
    DateTime dtLondon = dt.withZone(DateTimeZone.forID("Europe/London"));

Interval:

Interval interval = new Interval(start, end); //start and end are two DateTimes
查看更多
登录 后发表回答