Java 8 time zone ZoneRulesException: Unknown time-zone ID: EST

Sunnyday :

"EST" is one of the timeZone id from TimeZone.getAvailableIDs();

but

TimeZone.getAvailableIDs();  // contains EST
ZoneId.of("EST")

java.time.zone.ZoneRulesException: Unknown time-zone ID: EST

Andreas :

You are mixing old and new API's.

TimeZone.getAvailableIDs() returns Time Zone ID's that TimeZone.getTimeZone(String ID) can resolve.

ZoneId.getAvailableZoneIds() returns Zone ID's that ZoneId.of(String zoneId) can resolve.

If you compare the results of the 2, you will see:

public static void main(String[] args) {
    Set<String> timeZones = Set.of(TimeZone.getAvailableIDs());
    Set<String> zoneIds = ZoneId.getAvailableZoneIds();
    System.out.println("Extra TimeZone's: " + diff(timeZones, zoneIds));
    System.out.println("Extra ZoneId's: " + diff(zoneIds, timeZones));
}
static Set<String> diff(Set<String> a, Set<String> b) {
    Set<String> diff = new TreeSet<>(a);
    diff.removeAll(b);
    return diff;
}

Output (jdk-11.0.1)

Extra TimeZone's: [ACT, AET, AGT, ART, AST, BET, BST, CAT, CNT, CST, CTT, EAT, ECT, EST, HST, IET, IST, JST, MIT, MST, NET, NST, PLT, PNT, PRT, PST, SST, VST]
Extra ZoneId's: []

As you can see, ZoneId.getAvailableZoneIds() does not claim to support EST, only TimeZone.getAvailableIDs() does.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=141735&siteId=1
Recommended