问题
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("..."))
回答1:
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);
}
回答2:
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.
回答3:
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
.
回答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.
来源:https://stackoverflow.com/questions/42512290/where-is-the-official-list-of-zone-names-for-java-time