Steffen Harbich
Steffen Harbich

Reputation: 2749

Localized name of ZoneId's ID

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

Upvotes: 3

Views: 2396

Answers (1)

Manos Nikolaidis
Manos Nikolaidis

Reputation: 22224

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.

Upvotes: 7

Related Questions