Java如何获取给定时区缩写的时区ID列表 [英] Java how to get list of time zone ID’s for the given time zone abbreviation

查看:1250
本文介绍了Java如何获取给定时区缩写的时区ID列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以找到给定时区缩写的时区ID列表?例如,对于缩写 IST ,时区ID为亚洲/耶路撒冷亚洲/ Kolkata 欧洲/都柏林

Is it possible to find the list of time zone ID's for a given time zone abbreviation? For example, for the abbreviation IST, the time zone ID's are Asia/Jerusalem, Asia/Kolkata and Europe/Dublin.

推荐答案

<有趣的问题。由于缩写没有标准化,因此无法获得权威答案,也无法获得这样的答案。我想到了两个方法:

Interesting question. Since the abbreviations aren’t standardized, there cannot be an authoritative answer nor a bulletproof way to get such an answer. Off the top of my head I thought of two approaches:


  1. 从你的JVM中获取它们。

  2. 在网上找到它们。

从JVM获取区域:

    String givenAbbr = "IST";
    LocalDateTime summerSouthernHemisphere = LocalDate.of(2018, Month.JANUARY, 31).atStartOfDay();
    LocalDateTime summerNorthernHemisphere = LocalDate.of(2018, Month.JULY, 31).atStartOfDay();
    DateTimeFormatter dtf = DateTimeFormatter.ofPattern("z");
    Set<ZoneId> zones = new HashSet<>();
    for (String id : ZoneId.getAvailableZoneIds()) {
        ZoneId zone = ZoneId.of(id);
        String abbr = summerSouthernHemisphere.atZone(zone).format(dtf);
        if (abbr.equals(givenAbbr)) {
            zones.add(zone);
        }
        abbr = summerNorthernHemisphere.atZone(zone).format(dtf);
        if (abbr.equals(givenAbbr)) {
            zones.add(zone);
        }
    }
    System.out.println(zones);

这打印:

[Asia/Calcutta, Eire, Europe/Dublin, Asia/Jerusalem, Asia/Tel_Aviv, Israel, Asia/Kolkata, Asia/Colombo]

其中一些只是同一时区的名称。例如,Eire与欧洲/都柏林有相同的规则。因此,如果需要,可以进行进一步的过滤。您可以使用 oneZoneId.getRules()。equals(anotherZoneId.getRules())来确定两个 ZoneId 对象是否有相同的区域规则。

Some of these are just names for the same time zone, though. For example Eire has the same rules as Europe/Dublin. So a further filtering could be made if desired. You may use oneZoneId.getRules().equals(anotherZoneId.getRules()) to determine if two ZoneId objects have the same zone rules.

对于缩写CST,列表甚至更长并且有更多的同义词:

For abbreviation CST the list is even longer and has more synonyms:

[PRC, America/Matamoros, Asia/Taipei, America/Regina, America/El_Salvador,
        America/North_Dakota/New_Salem, Asia/Harbin, America/Costa_Rica,
        America/North_Dakota/Center, America/Guatemala, America/Winnipeg,
        Asia/Chongqing, America/Rankin_Inlet, America/Indiana/Knox,
        America/Belize, SystemV/CST6CDT, Mexico/General,
        America/North_Dakota/Beulah, CST6CDT, America/Swift_Current,
        America/Knox_IN, Asia/Chungking, Asia/Macao, Asia/Shanghai,
        America/Indiana/Tell_City, America/Menominee, America/Bahia_Banderas,
        America/Managua, Canada/East-Saskatchewan, Asia/Macau, America/Havana,
        America/Resolute, US/Central, US/Indiana-Starke, Cuba, America/Monterrey,
        America/Chicago, America/Merida, America/Mexico_City, Canada/Central,
        America/Tegucigalpa, America/Rainy_River, Canada/Saskatchewan, SystemV/CST6]

我的方法的一个限制是一些时区被多个名称所知,因此不止一个三四个字母的缩写。我上面的代码只捕获其中的一个。

One limitation of my approach is that some time zones are known by more than one name and therefore more than one three or four letter abbreviation. My code above catches only one of these.

另一个限制是选择像我这样的两个日期将永远不会给你所有可能的过去和未来,甚至可能想念一些我没有碰到正确日期的地方。我试图选择一个日期,它是北半球的冬天和南半球的夏天,另一个是相反的日期。这将涵盖目前的大多数情况,但你永远不知道是否有一个时区或三个时间段过渡不遵循我们所知的夏季和冬季。如果您想要更好的报道,请在 Hugo的回答中提出一些很好的建议。

Another limitation is that picking two dates like I do will never give you all possibilites in the past and the future, and may even miss some where I just don’t hit the right date. I have tried to pick one date where it is winter on the northern hemisphere and summer on the southern, and one where it is the other way around. This will cover most cases for the present, but you never know if there is a time zone or three where the transition don’t follow summer and winter as we know it. If you want better coverage, there are a couple of excellent suggestions in Hugo’s answer.

另一个答案当然是您的搜索无疑已提出的答案:此类列表是公开的在网上。例如,维基百科的时区缩写列表时区缩写 - timeanddate.com上的全球列表。正如所料,提到的两个名单不同意。例如,后者知道ADT的两种解释,前者只有一种。后面的列表给出了许多同义词缩写,从而说明了我的观点,即每个区域可以有多个缩写。

The other answer is, of course, the one that your search has no doubt already brought up: such lists are public on the Internet. For example Wikipedia’s List of time zone abbreviations and Time Zone Abbreviations – Worldwide List on timeanddate.com. As expected, the two lists mentioned do not agree. For example, the latter knows two interpretations of ADT, the former only one. The latter list gives many synonym abbreviations and thereby illustrates my point above that each zone can have more than one abbreviation.

这篇关于Java如何获取给定时区缩写的时区ID列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆