Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does joda DateTimeZone's getOffset() method require an "instant"?

What I'm trying to do:

From a DateTimeZone object, I'm trying to get the GMT offset in milliseconds. Example:

DateTimeZone gmt= // somehow get gmt zone object
long offset = gmt.getOffsetSomehow(); // expect offset = 0

DateTimeZone ny_est = // somehow get that time zone object representing "EST" offset of NY zone
long offset = ny_est.getOffsetSomehow(); // expect offset = -18000000 = -5*60*60*1000

DateTimeZone hkt = // somehow get HK time zone object
long offset = hkt.getOffsetSomehow(); // expect offset = 28800000 = 8*60*60*1000

Joda's DateTimeZone object has a method called getOffset, which accepts a parameter. Question:

  1. why does that method need a parameter? I would have expected the method to not expect any param, and simply behave how I want my fictitious "getOffsetSomehow()" method.
  2. How can I get the time zone objects in my snippet above?
  3. What actual method/snippet of code can get me the "offset" values I'm try to get?
like image 684
jrharshath Avatar asked Mar 25 '13 10:03

jrharshath


1 Answers

This is, because the Offset changes over time. One example is daylight saving time. Another is a country deciding to move to another timezone.

If you want the current offset, just hand in now like this:

int offset = DateTimeZone.forID("Europe/Berlin").getOffset(new DateTime());
System.out.println(offset);
like image 110
Tim Büthe Avatar answered Sep 30 '22 05:09

Tim Büthe