Where is the official list of zone names for java.time?
Asked Answered
P

5

25

Is there an official list of zone names that are used for the following:

zoneId = ZoneId.of("America/New_York")

Or is it possible for java.time.ZoneId to generate the list itself that I can use?

I'm using this to convert an Instant to a string representation of time like:

ZoneDateTime.ofInstant(instant, zoneId).format(DateTimeFormatter.ofPattern("..."))
Pandybat answered 28/2, 2017 at 15:8 Comment(0)
K
22

As other answers have said, the way to get this list in your code is to use ZoneId.getAvailableZoneIds().

However, if you just to browse want an "official" list, the data is available from IANA, but this is in a format intended for machines. The easiest way to peruse the data is on Wikipedia. (With usual disclaimer that Wikipedia is Wikipedia...)

You should also be aware that recent changes to IANA/Wikipedia data might not be present in your target JRE if it has not been updated recently.

Katiakatie answered 12/2, 2019 at 15:22 Comment(1)
Upvoted because you provided a way not mentioned in other answers. All other answers assume that the reader has access to a JVM.Algorithm
F
17

Just use the getAvailableZoneIds method.

Gets the set of available zone IDs.

This set includes the string form of all available region-based IDs. Offset-based zone IDs are not included in the returned set. The ID can be passed to of(String) to create a ZoneId.

Set<String> zoneIds= ZoneId.getAvailableZoneIds();

for (String zone : zoneIds) {
    System.out.println(zone);
}
Forth answered 28/2, 2017 at 15:14 Comment(0)
C
11

Is there an official list of zone names that are used for the following:

No, Java doesn't maintain any official list of zone names. It rather gets that information from IANA. You can also specify JVM to use your custom zone name information file to override the one from IANA.

JVM loads the time zone information from a binary file called tzdb.dat which is located at the following place:

$JAVA_HOME\lib

This file in turn is generated at the time of JRE installation from IANA.

If IANA updates the time zone information for some reasons and you want to update JVM's cached copy (tzdb.dat), you can do it using a utility JVM provides called tzupdater.jar. Alternately, you can also reinstall JRE.

Lastly, if you don't trust the time zone information data in tzdb.dat which is obtained from IANA's site, you can provide your custom timezone information file by specifying a system property called java.time.zone.DefaultZoneRulesProvider.

Cassock answered 28/2, 2017 at 16:5 Comment(4)
ZoneId.getAvailableZoneIds() is available. IMO Your post seems to go on a tangent and explains the inner workings, not related to the questionQuinton
@BojanPetkovic, the method getAvailableZoneIds doesn't have a list of zones or doesn't maintain one. It is simply a getter. Read my answer again to find where or how the list is maintained. The other answer is insufficient despite it has so many upvotes.Cassock
I do see your point, however I think you are are reading a bit too much into the question. What I read was for ZoneId.of(XXX) what values are allowed. I wi ll remove the down vote as it does provide some information that is correct, just not needed for the questionQuinton
Your vote is locked once cast. Please be courteous next time before down voting a useful answer like this.Cassock
E
4

One can obtain this list either through using the Java ZoneId or TimeZone Classes.

The ZoneId Class is defined here:

https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html

The following code will print all the time zones using the ZoneId Class

package com.javadb;

import java.time.ZoneId;
import java.util.TreeSet;

/**
 * Displays available time zones (zoneIds)
 *
 * @author www.javadb.com
 */
public class DisplayTimeZones {

    public static void main(String[] args) {

        TreeSet<String> sortedZones = new TreeSet<>(ZoneId.getAvailableZoneIds());

        System.out.println("Number of zones: " + sortedZones.size());
        System.out.println("");

        for (String zone : sortedZones) {
        System.out.println(zone);
       }

    //Or if you want to use functional operations
    /*
    sortedZones.stream().forEach((zone) -> {
        System.out.println(zone);
    });
    */
    } 
}

Output:

Number of zones: 585

Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
...

Using the Java TimeZone class java.util.TimeZone the list can be obtained by calling:

TimeZone.getAvailableIDs()

In addition, if one uses Joda-Time, the Class DateTimeZone getAvailableIDs() will work as well.

Eucken answered 28/2, 2017 at 15:12 Comment(2)
The question is not about Joda-Time so you should avoid this term in given context (and your code does not use Joda-Time, too). Meaning, you should avoid the impression that Joda-Time and java.time-API are the same or similar. They are really different.Sesquipedalian
@MenoHochschild - Thanks, I fixed my answer to be consistent.Eucken
C
3

I found this online list of Zone IDs by Gary Gregory quite helpful.

Columnar answered 11/9, 2021 at 6:35 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ageratum

© 2022 - 2024 — McMap. All rights reserved.