java 时区 列表,Java如何获取给定时区缩写的时区ID列表

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:

Get them from your JVM.

Find them on the net.

Getting the zones from your 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 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);

This prints:

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

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.

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.

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.

Get them from the Internet

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值