如何使用Java 8获取所有TimeZones ID的列表?
要检索所有可用时区ID的列表,我们可以调用java.time.ZoneIdstatic方法getAvailableZoneIds()。此方法返回Set所有区域ID的字符串a。区域ID的格式为“{area}/{city}”。可以使用ZoneId.of()静态方法使用这个字符串id创建ZoneId对象。
package org.nhooo.example.datetime; import java.time.ZoneId; import java.time.format.TextStyle; import java.util.Locale; import java.util.Set; public class GetAllTimeZoneIds { public static void main(String[] args) { Set<String> zoneIds = ZoneId.getAvailableZoneIds(); for (String id : zoneIds) { ZoneId zoneId = ZoneId.of(id); System.out.println("id = " + id); System.out.println("displayName = " + zoneId.getDisplayName(TextStyle.FULL, Locale.US)); } } }
以下是一些打印到控制台的区域ID:
id = Asia/Aden displayName = Arabian Time id = America/Cuiaba displayName = Amazon Time id = Etc/GMT+9 displayName = GMT-9:00 id = Etc/GMT+8 displayName = GMT-8:00 id = Africa/Nairobi displayName = Eastern Africa Time ... ... ... id = Europe/Nicosia displayName = Eastern European Time id = Pacific/Guadalcanal displayName = Solomon Is. Time id = Europe/Athens displayName = Eastern European Time id = US/Pacific displayName = Pacific Time id = Europe/Monaco displayName = Central European Time