Arizona TimeZone Day Light Saving

后端 未结 4 1009
悲哀的现实
悲哀的现实 2021-01-06 13:26

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

Calendar j = new GregorianCalendar(TimeZone.getTimeZone(\"US/Mountain\"));
j.         


        
4条回答
  •  情歌与酒
    2021-01-06 13:52

    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.

提交回复
热议问题