使用ZoneId的下拉列表过长,如何简化列表?
使用Spring ZoneId
,代码:使用ZoneId的下拉列表过长,如何简化列表?
private static Map<String, String> getAllZoneIds() {
final List<String> zoneList = new ArrayList<>(ZoneId.getAvailableZoneIds());
final Map<String, String> zones = new HashMap<>();
final LocalDateTime dt = LocalDateTime.now();
for (final String zoneId : zoneList) {
final ZoneId zone = ZoneId.of(zoneId);
final ZonedDateTime zdt = dt.atZone(zone);
final ZoneOffset zos = zdt.getOffset();
//replace Z to +00:00
final String offset = zos.getId().replaceAll("Z", "+00:00");
zones.put(zone.toString(), offset);
}
final Map<String, String> sortZones = new LinkedHashMap<>();
zones.entrySet().stream().sorted((left, right) -> {
final String leftValue = left.getValue();
final String leftKey = left.getKey();
final String rightValue = right.getValue();
final String rightKey = right.getKey();
if (leftValue.equalsIgnoreCase(rightValue)) {
return leftKey.compareTo(rightKey);
} else {
if (leftValue.charAt(0) == '+' && leftValue.charAt(0) == rightValue.charAt(0)) {
return leftValue.compareTo(rightValue);
} else {
return rightValue.compareTo(leftValue);
}
}
}).forEachOrdered(e -> {
sortZones.put(e.getKey(), e.getKey() + " (UTC" + e.getValue() + ")");
});
System.out.println(sortZones);
return sortZones;
}
输出是:
....
Antarctica/Casey->Antarctica/Casey (UTC+08:00)
Asia/Brunei->Asia/Brunei (UTC+08:00)
Asia/Chongqing->Asia/Chongqing (UTC+08:00)
Asia/Chungking->Asia/Chungking (UTC+08:00)
Asia/Harbin->Asia/Harbin (UTC+08:00)
Asia/Hong_Kong->Asia/Hong_Kong (UTC+08:00)
Asia/Hovd->Asia/Hovd (UTC+08:00)
Asia/Irkutsk->Asia/Irkutsk (UTC+08:00)
Asia/Kuala_Lumpur->Asia/Kuala_Lumpur (UTC+08:00)
Asia/Kuching->Asia/Kuching (UTC+08:00)
Asia/Macao->Asia/Macao (UTC+08:00)
Asia/Macau->Asia/Macau (UTC+08:00)
Asia/Makassar->Asia/Makassar (UTC+08:00)
Asia/Manila->Asia/Manila (UTC+08:00)
Asia/Shanghai->Asia/Shanghai (UTC+08:00)
Asia/Singapore->Asia/Singapore (UTC+08:00)
Asia/Taipei->Asia/Taipei (UTC+08:00)
Asia/Ujung_Pandang->Asia/Ujung_Pandang (UTC+08:00)
Australia/Perth->Australia/Perth (UTC+08:00)
Australia/West->Australia/West (UTC+08:00)
Etc/GMT-8->Etc/GMT-8 (UTC+08:00)
Hongkong->Hongkong (UTC+08:00)
PRC->PRC (UTC+08:00)
Singapore->Singapore (UTC+08:00)
Asia/Pyongyang->Asia/Pyongyang (UTC+08:30)
....
不过这个名单太长,它包含超过500个选项,它充满了重复的。例如,重庆存在两次:
Asia/Chongqing->Asia/Chongqing (UTC+08:00)
Asia/Chungking->Asia/Chungking (UTC+08:00)
如何使用它作为较短的列表?
由于backward file,某些区域出现两次:该文件映射时区名称已更改,但旧名称保留为同义词,可能是由于逆向兼容性原因。如果您在文件看看,你会发现这些条目:
Link Asia/Shanghai Asia/Chongqing
Link Asia/Shanghai Asia/Chungking
这意味着Asia/Shanghai
是Asia/Chungking
和Asia/Chongqing
时区当前时区的名字。检查的一种方式,如果两个区域是相同的是比较它们各自的ZoneRules
:
ZoneId chungking = ZoneId.of("Asia/Chungking");
ZoneId chongqing = ZoneId.of("Asia/Chongqing");
ZoneId shanghai = ZoneId.of("Asia/Shanghai");
System.out.println(chungking.getRules().equals(chongqing.getRules()));
System.out.println(chungking.getRules().equals(shanghai.getRules()));
两个比较打印true
,这意味着上述所有ZoneId
对象表示相同的时区。遗憾的是,直接比较ZoneId
对象不起作用,只有比较ZoneRules
,我们才能知道两个区域是否相同。
所以减少列表的一种方法是忽略同义词。如果两个或更多区域具有相同的ZoneRules
,则可以将其中的一个放在列表中。
对于这一点,你可以创建一个包装了ZoneId
,并具有equals
方法比较ZoneRules
的辅助类(也是一个hashcode
方法,因为它是一个good practice to properly implement both)。
public class UniqueZone {
private ZoneId zone;
public UniqueZone(ZoneId zone) {
this.zone = zone;
}
public ZoneId getZone() {
return zone;
}
// hashcode and equals use the ZoneRules
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((zone == null) ? 0 : zone.getRules().hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (!(obj instanceof UniqueZone))
return false;
UniqueZone other = (UniqueZone) obj;
if (zone == null) {
if (other.zone != null)
return false;
// two zones are equal if the ZoneRules are the same
} else if (!zone.getRules().equals(other.zone.getRules()))
return false;
return true;
}
}
然后,我创建UniqueZone
的Set
一个实例中,使用所有区域ID可用:
Set<UniqueZone> uniqueZones = ZoneId
// get all IDs
.getAvailableZoneIds().stream()
// map to a UniqueZone (so zones with the same ZoneRules won't be duplicated)
.map(zoneName -> new UniqueZone(ZoneId.of(zoneName)))
// create Set
.collect(Collectors.toSet());
这将创建的UniqueZone
唯一实例Set
,利用在equals
方法中定义的标准(如果两个ZoneId
实例具有相同的ZoneRules
,则它们被认为是相同的,并且只有一个插入Set
)。
现在我们只需要映射这个Set
回List
:
List<String> zoneList = uniqueZones.stream()
// map back to the zoneId
.map(u -> u.getZone().getId())
// create list
.collect(Collectors.toList());
现在你可以使用zoneList
您已经使用了同样的方式。
用上述代码,在JDK 1.8.0_144,列表385层的元件,并且仅Asia/Chungking
是在列表中。在使用流时,您无法真正预测这三个中的哪一个(Asia/Chungking
,Asia/Chongqing
或Asia/Shanghai
)将会出现在列表中。
如果您想对其进行一些控制,可以通过创建要排除的名称列表来明确地过滤不想要的名称。假设我想排除Asia/Chungking
和Asia/Chongqing
(因此只有Asia/Shanghai
将在列表中)。我可以这样做:
// zone names to be excluded
Set<String> excludedNames = new HashSet<>();
excludedNames.add("Asia/Chungking");
excludedNames.add("Asia/Chongqing");
Set<UniqueZone> uniqueZones = ZoneId
// get all IDs
.getAvailableZoneIds().stream()
// filter names I don't want
.filter(zoneName -> ! excludedNames.contains(zoneName))
// map to a UniqueZone (so zones with the same ZoneRules won't be duplicated)
.map(zoneName -> new UniqueZone(ZoneId.of(zoneName)))
// create Set
.collect(Collectors.toSet());
这样,只有Asia/Shanghai
将在列表中。
PS:如果排除名称的列表足够长,以覆盖所有的代名词情况下,你甚至都不需要UniqueZone
类(过滤器会做所有的工作)。但是您需要事先知道排除的所有同义词。
等等都是新加坡 – daxue