开发者

Arizona TimeZone Day Light Saving

开发者 https://www.devze.com 2023-01-23 09:47 出处:网络
I am trying to get current time in specific time zones. I tried following code. Calendar j = new GregorianCalendar(TimeZone.getTimeZone(\"US/Mountain\"));

I am trying to get current time in specific time zones. I tried following code.

Calendar j = new GregorianCalendar(TimeZone.getTimeZone("US/Mountain"));
j.setTimeInMillis(Calendar.getInstance().getTimeInMillis());
System.out.println(j.get(Calendar.HOUR_OF_DAY)+":"+j.get(Calendar.MINUTE));
TimeZone tz = TimeZone.getTimeZone("US/Mountain");
System.out.println("tz.getRawOffset()"+tz.getRawOffset()/3600);
System.out.println(tz.getDSTSavings());
System.out.println

(tz.inDaylightTime(new Date()));

The answer I got is surprising - 16:57 - wrong by 1 hr tz.getRawOffset()-7000 3600开发者_Python百科000 true - why daylight saving is true for Arizona? How to get the correct wall clock time of Phoenix or any other city in US?


Arizona is in the Mountain timezone, but doesn't observe DST. If you specify the timezone "US/Mountain", then the computer will apply the rules used by most states in the Mountain time zone, which include observing daylight savings time. To get the rules for Arizona (which don't include DST), you want the timezone "US/Arizona" (or "America/Phoenix"). In the Navajo nation, you want the timezone named "Navajo".

To save yourself some of the trouble, always try to use the names from "America/*" where you can pick the name of a city that has the same timezone rules as the place you're interested in.

To get the correct time in the correct timezone for any given city in the world, you simply have to familiarize yourself with the names in the Olson timezone database and their meanings. While you usually think of the term "time zone" to mean the time of day in the middle of the winter (when everybody observes standard time), in the Olson database a timezone name represents the entire history of daylight savings time rules and timezone rules for a particular region.

As an example, even though Indiana now observes Eastern time and observes DST (except for a few counties right near Chicago which are on Central time like Chicago), before 2006 they didn't observe DST. A timezone named "US/Indiana" (or "America/Indianapolis") was created to cover this region, an even today, you would still want to use the timezone "America/Indianapolis" when talking about Indiana, so that queries about dates and times before 2006 could be answered correctly.


Most of Arizona does not observe Daylight Savings Time, but the Navajo Nation inside Arizona does observe DST.


The zone information can be obtained directly for many major cities worldwide. This includes Phoenix, which has the zone identifier "America/Phoenix".

You can find the list of available zone identifiers using the method TimeZone.getAvailableIDs() or by manually inspecting the contents of the JRE lib/zi directory.

As other posters have noted, the "US/Arizona" zone information is distinct from "US/Mountain".


I am providing the modern answer. Don’t use the classes Calendar, GregorianCalendar, TimeZone and Date. They are all poorly designed and fortunately all long outdated.

java.time

It’s simple when you know how:

    ZonedDateTime arizonaExceptNavajo = ZonedDateTime.now(ZoneId.of("America/Phoenix"));
    System.out.println(arizonaExceptNavajo);

When I ran this code just now, the output was:

2019-10-23T04:07:23.034-07:00[America/Phoenix]

The US/Mountain time zone ID is deprecated. It’s a link to America/Denver, and America/Denver does use summer time (daylight saving time, DST). Modern time zone IDs have the form region/city where region is either a continent like America or an ocean like Pacific.

As others have said, summer time is used on one place in Arizona, the Navajo Nation. The Navajo time zone ID mentioned is deprecated too. Use America/Denver:

    ZonedDateTime navajoNation = ZonedDateTime.now(ZoneId.of("America/Denver"));
    System.out.println(navajoNation);

2019-10-23T05:07:23.037-06:00[America/Denver]

Since summer time is still in effect, the time of day is one hour ahead compared to the one for Phoenix above.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Navajo Nation on Wikipedia.
0

精彩评论

暂无评论...
验证码 换一张
取 消