Localized name of ZoneId's ID

ε祈祈猫儿з 提交于 2021-02-19 03:45:08

问题


I have a list of time zones that I want the user to choose from. So, I thought I can just call java.time.ZoneId.getAvailableZoneIds() and use the method getDisplayName on them. This results in a lot of duplicate entries like

Central European Time

Even if I add the time zone offset they are not unqiue. However, the ID of a ZoneId distinguishes the entries but how can I localize them? The IDs are always in English like

Europe/Rome


回答1:


It's possible to get a localized version of the display name by calling getDisplayName on a ZoneId instance. That would require iteration over the result of getAvailableZoneIds():

ZoneId.getAvailableZoneIds().stream()
        .map(ZoneId::of)
        .map(zid -> zid.getDisplayName(TextStyle.FULL, Locale.GERMAN))
        .distinct()
        .forEach(System.out::println);

Note the TextStyle argument to change the size of each zone's title and the .distinct() method to get unique results.



来源:https://stackoverflow.com/questions/47070956/localized-name-of-zoneids-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!