Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

timezone

"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

like image 470
eastwater Avatar asked Aug 12 '19 21:08

eastwater


People also ask

What are the Java TimeZone IDs?

A ZoneId is used to identify the rules used to convert between an Instant and a LocalDateTime . There are two distinct types of ID: Fixed offsets - a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times.

How do I get a Zone ID?

To find your zone and account IDs: Log in to the Cloudflare dashboard Open external link and select your account and domain. On the Overview page (the landing page for your domain), find the API section. The API section contains your Zone ID and Account ID.

How do I change the TimeZone in Java 8?

Typically, you get a TimeZone using getDefault which creates a TimeZone based on the time zone where the program is running. For example, for a program running in Japan, getDefault creates a TimeZone object based on Japanese Standard Time. Hours must be between 0 to 23 and Minutes must be between 00 to 59.

What is a TimeZone ID?

The Time zone ID column lists time zones, in boldface, and the locations within each time zone. The Raw offset column lists the difference, in hours and minutes, between Greenwich Mean Time (GMT) and the specified time zone. The DST offset column lists the offset, in minutes, for Daylight Savings Time (DST).


2 Answers

Try this:

ZoneId.of("EST", ZoneId.SHORT_IDS);

Explanation: ZoneID.SHORT_IDS maps deprecated 3-letter codes to regular ones and for some reason it must be specified explicitely to take effect.

From JDK javadoc: "Use of short zone IDs has been deprecated in TimeZone".

like image 133
martins Avatar answered Oct 27 '22 00:10

martins


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.

like image 29
Andreas Avatar answered Oct 26 '22 22:10

Andreas