Using Joda time to get current wall time for a given Time Zone

China☆狼群 提交于 2019-12-31 10:39:33

问题


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.


回答1:


Have you looked at the DateTime constructor:

DateTime(DateTimeZone zone) 

This constructs a DateTime representing the current time in the specified timezone.




回答2:


How about:

DateTime utc = new DateTime(DateTimeZone.UTC);
DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
DateTime losAngelesDateTime = utc.toDateTime(tz);



回答3:


Cleanest Method

I find the following to be the cleanest method.

DateTime currentTime = DateTime.now( DateTimeZone.UTC );

This gets you the current time in UTC but this value can be converted to another DateTimeZone or you can replace DateTimeZone.UTC with a different DateTimeZone.

Using the System TimeZone

If you want to set it to the system timezone, you can use this:

DateTime currentTime = DateTime.now( DateTimeZone.getDefault() );


来源:https://stackoverflow.com/questions/9184499/using-joda-time-to-get-current-wall-time-for-a-given-time-zone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!