亚利桑那州时区夏令时
我正在尝试获取特定时区的当前时间。我尝试了以下代码。
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()));
得到的答案却让我大吃一惊—— 16:57 - 错误 1 小时 tz.getRawOffset()-7000 3600000 正确 - 为什么亚利桑那州实行夏令时? 如何获得凤凰城或美国任何其他城市的正确挂钟时间?
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
3600000
true - why daylight saving is true for Arizona?
How to get the correct wall clock time of Phoenix or any other city in US?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
亚利桑那州位于山区时区,但不遵守夏令时。如果您指定时区“美国/山区”,则计算机将应用山区时区大多数州使用的规则,其中包括遵守夏令时。要获取亚利桑那州的规则(不包括 DST),您需要时区“US/Arizona”(或“America/Phoenix”)。在纳瓦霍民族中,您希望时区名为“纳瓦霍”。
避免一些麻烦,请始终尝试使用“America/*”中的名称,您可以在其中选择与您感兴趣的地方具有相同时区规则的城市的名称。
为了 要了解世界上任何给定城市的正确时区,您只需熟悉奥尔森时区数据库中的名称及其含义即可。虽然您通常认为术语“时区”是指冬季中期的时间(此时每个人都遵守标准时间),但在 Olson 数据库中,时区名称代表夏令时规则和时区规则的整个历史对于特定区域。
例如,尽管印第安纳州现在遵守东部时间并遵守夏令时(除了少数地区)芝加哥附近的县(像芝加哥一样采用中部时间),在 2006 年之前他们没有遵守夏令时。创建了名为“US/Indiana”(或“America/Indianapolis”)的时区来覆盖该地区,即使在今天,您在谈论印第安纳州时仍然希望使用时区“America/Indianapolis”,以便查询日期2006年之前的时间都可以正确回答。
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.
我正在提供现代答案。不要使用
Calendar
、GregorianCalendar
、TimeZone
和Date
类。它们的设计都很糟糕,幸运的是都已经过时了。java.time
当你知道怎么做时就很简单了:
当我刚才运行这段代码时,输出是:
美国/山区时区 ID 已弃用。它是到美国/丹佛的链接,而美国/丹佛确实使用夏令时(夏令时,DST)。现代时区 ID 的格式为 地区/城市,其中 地区 可以是美洲大陆,也可以是太平洋等海洋。
正如其他人所说,亚利桑那州的一个地方纳瓦霍族国家使用夏令时。提到的纳瓦霍时区 ID 也已弃用。使用美国/丹佛:
由于夏令时仍然有效,因此与上面凤凰城的时间相比早一小时。
链接
I am providing the modern answer. Don’t use the classes
Calendar
,GregorianCalendar
,TimeZone
andDate
. They are all poorly designed and fortunately all long outdated.java.time
It’s simple when you know how:
When I ran this code just now, the output was:
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:Since summer time is still in effect, the time of day is one hour ahead compared to the one for Phoenix above.
Links
可以直接获取全球许多主要城市的区域信息。这包括凤凰城,其区域标识符为“America/Phoenix”。
您可以使用 TimeZone.getAvailableIDs() 方法或手动检查 JRE lib/zi 目录的内容来查找可用区域标识符的列表。
正如其他发帖者所指出的,“美国/亚利桑那”区域信息与“美国/山区”不同。
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".
输出:
当地时间:08:43 AM
亚利桑那州等效时间 : 01:43 AM
MST 等效时间:02:43 AM
但是,如果我们更改日历而不更改日期格式,则时区格式的输出将相同(MST、US/Arizona、US/Mountain)。
输出:
当地时间 1:08:56 AM
当地时间 2: 08:56 AM
当地时间凌晨 3:08:56
当地时间 4:08:56 AM
Output :
Local time : 08:43 AM
Arizona equivalent time : 01:43 AM
MST equivalent time : 02:43 AM
But if we change the Calendar without changing the Dateformat , the output will be same for the Timezone-Format(MST, US/Arizona , US/Mountain ).
Output :
Local time 1: 08:56 AM
Local time 2: 08:56 AM
Local time 3: 08:56 AM
Local time 4: 08:56 AM