问题
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