The requirement is to simply to get the current wall time (including the correct DST adjustment) for a given Time Zone.
There seems to be a few questions in SO hovering around this, but I can't seem to find a straight answer (in SO, Joda doco or googling) with a low friction way of getting the wall time. It seems like with the given inputs (current UTC time and desired TZ) I should be able to chain a couple methods from the Joda Time library to achieve what I want but there seems to be a desire in said examples to evaluate + handle offsets / transitions in application code - I want to avoid this if possible and just use Jodas best effort based on its set of static TZ rules available.
For the purposes of this question, lets assume that I wont be using any other third party services (network based or other binaries), just what is available from the JDK and JodaTime library.
Any pointers appreciated.
Update: This was actually a gaffe on my behalf. I had a calculated UTC offset based on the requests longitude, while this works obviously you still need regional information to get the correct DST adjustment..
double aucklandLatitude = 174.730423;
int utcOffset = (int) Math.round((aucklandLatitude * DateTimeConstants.HOURS_PER_DAY) / 360);
System.out.println("Offset: " + utcOffset);
DateTimeZone calculatedDateTimeZone = DateTimeZone.forOffsetHours(utcOffset);
System.out.println("Calculated DTZ: " + calculatedDateTimeZone);
System.out.println("Calculated Date: " + new DateTime(calculatedDateTimeZone));
System.out.println();
DateTimeZone aucklandDateTimeZone = DateTimeZone.forID("Pacific/Auckland");
System.out.println("Auckland DTZ: " + aucklandDateTimeZone);
System.out.println("Auckland Date: " + new DateTime(aucklandDateTimeZone));
prints
Offset: 12
Calculated DTZ: +12:00
Calculated Date: 2012-02-08T11:20:04.741+12:00
Auckland DTZ: Pacific/Auckland
Auckland Date: 2012-02-08T12:20:04.803+13:00
So here in sunny Auckland, NZ we are +12 but +13 in the DST period.
My bad. Thanks for the answers nonetheless, made me see my mistake.
Have you looked at the DateTime
constructor:
DateTime(DateTimeZone zone)
This constructs a DateTime representing the current time in the specified timezone.
How about:
DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
DateTime losAngelesDateTime = utc.toDateTime(tz);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With