List possible Time Zones or ZoneIds in Java

Sometimes you might need to get the date and time for a specific time zone. A specific case would be when you need to pass in the ZoneId to one of the classes LocalDate, LocalTime or LocalDateTime. This code example shows which ZoneIds that can be used by fetching them from the system and printing them out.

The code below fetches the ZoneIds with a call to the static method getAvailableZoneIds() in the ZoneId class. This method returns a Set of Strings. This Set is of type HashSet and therefore the content is not sorted. To get a sorted Set we create an instance of TreeSet to which we send the HashSet as parameter to the constructor. This enables us to loop through the TreeSet to print out a sorted list of zoneIds.

The output from the code above is the number of zoneIds followed by a sorted list.

Search for more Java info on this site here:
Custom Search

Any thoughts or questions?