Difference between time zone IDs using TimeZone and ZoneId classes

Viewed 1766

In my program, user can choose a time zone from a list of IDs that I get from TimeZone.getAvailableIDs(). This ID is stored in the DB and later I use ZoneId.of(String zoneId) to get the ZoneId Object.

But today while debugging I realized that some IDs generated by TimeZone.getAvailableIDs() are not recognized by ZoneId.of(String zoneId) and a "java.time.zone.ZoneRulesException" is thrown. For example ZoneId.of("EAT") throws an exception.

The ZoneId class itself can generate a list of zone IDs using ZoneId.getAvailableZoneIds(). this returns 599 Ids for me but TimeZone.getAvailableIDs() returns 627 IDs.

Can someone please explain the difference to me? Why am I getting 2 different lists of zone IDs?

I use Java 8.

1 Answers

From the docs of ZoneId

Use of short zone IDs has been deprecated in java.util.TimeZone. This map allows the IDs to continue to be used via the of(String, Map) factory method.

So you should use a different signature of the same method

ZoneId.of("EAT", ZoneId.SHORT_IDS)

or you can use the full name

ZoneId.of("Africa/Addis_Ababa")
Related