ZoneId ID的本地化名称 [英] Localized name of ZoneId's ID
问题描述
我有一个希望用户选择的时区列表.因此,我以为我可以调用java.time.ZoneId.getAvailableZoneIds()
并在它们上使用方法getDisplayName
.这导致很多重复的条目,例如
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
即使我添加了时区偏移量,它们也不是没有道理的.但是,ZoneId
的ID可以区分条目,但是我如何对它们进行本地化? ID始终为英文,例如
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
推荐答案
可以通过在ZoneId
实例上调用getDisplayName
来获得显示名称的本地化版本.这将需要对getAvailableZoneIds()
:
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);
请注意TextStyle
参数可更改每个区域标题的大小,而.distinct()
方法可获取唯一的结果.
Note the TextStyle
argument to change the size of each zone's title and the .distinct()
method to get unique results.
这篇关于ZoneId ID的本地化名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!