Java 8 – How to display Zones for particular Offset ?

In this article, we will discuss about how to display Zones for particular Offset like “Z” or “+00:00” or GMT/UTC

1. Display Zones for particular Offset :

  • ZoneId.getAvailableZoneIds(); provides all Zones in the form of Set<String>
  • To get Zones for particular Offset like “Z” or +00:00 or UTC/GMT,
    • get stream from Set<String> for Zones
    • process using different Stream methods like map(), filter(), sorted(), collect() and forEach() as shown in the below illustration

DisplayAllZonesForOffsetZ.java

package in.bench.resources.instant;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;

public class DisplayAllZonesForOffsetZ {

	public static void main(String[] args) {

		// 1. get all zones
		Set<String> zones = ZoneId.getAvailableZoneIds();


		// 1.1 print to console
		System.out.println("Zones with Offset Z or +00:00 or UTC/GMT :- \n");


		// 2. extract zones with Offset Z (+00:00) and print to console
		zones // original source
		.stream() // get stream
		.map(zone -> ZonedDateTime.now(ZoneId.of(zone))) // convert to ZonedDateTime
		.filter(zdt -> zdt.getOffset().toString().equalsIgnoreCase("Z")) // Filter Offset with Z
		.sorted((zdt1, zdt2) -> zdt1.getZone().toString().compareTo(zdt2.getZone().toString())) // sorting
		.forEach(zdt -> System.out.println(zdt.getOffset() + "\t" + zdt.getZone())); // printing
	}
}

Output:

Zones with Offset Z or +00:00 or UTC/GMT :- 

Z	Africa/Abidjan
Z	Africa/Accra
Z	Africa/Bamako
Z	Africa/Banjul
Z	Africa/Bissau
Z	Africa/Conakry
Z	Africa/Dakar
Z	Africa/Freetown
Z	Africa/Lome
Z	Africa/Monrovia
Z	Africa/Nouakchott
Z	Africa/Ouagadougou
Z	Africa/Sao_Tome
Z	Africa/Timbuktu
Z	America/Danmarkshavn
Z	America/Scoresbysund
Z	Atlantic/Azores
Z	Atlantic/Reykjavik
Z	Atlantic/St_Helena
Z	Etc/GMT
Z	Etc/GMT+0
Z	Etc/GMT-0
Z	Etc/GMT0
Z	Etc/Greenwich
Z	Etc/UCT
Z	Etc/UTC
Z	Etc/Universal
Z	Etc/Zulu
Z	GMT
Z	GMT0
Z	Greenwich
Z	Iceland
Z	UCT
Z	UTC
Z	Universal
Z	Zulu

2. Display Zones in Reverse Order :

  • This illustration is exactly same like previous example except that Zones are reverse alphabetically sorted

DisplayInReverseForAllZonesWithOffsetZ.java

package in.bench.resources.instant;

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Set;

public class DisplayInReverseForAllZonesWithOffsetZ {

	public static void main(String[] args) {

		// 1. get all zones
		Set<String> zones = ZoneId.getAvailableZoneIds();


		// 1.1 print to console
		System.out.println("Zones with Offset Z or +00:00 or UTC/GMT :- \n");


		// 2. extract zones with Offset Z (+00:00) and print to console
		zones // original source
		.stream() // get stream
		.map(zone -> ZonedDateTime.now(ZoneId.of(zone))) // convert to ZonedDateTime
		.filter(zdt -> zdt.getOffset().toString().equalsIgnoreCase("Z")) // Filter Offset with Z
		.sorted((zdt1, zdt2) -> zdt2.getZone().toString().compareTo(zdt1.getZone().toString())) // reverse
		.forEach(zdt -> System.out.println(zdt.getOffset() + "\t" + zdt.getZone())); // printing
	}
}

Output:

Zones with Offset Z or +00:00 or UTC/GMT :- 

Z	Zulu
Z	Universal
Z	UTC
Z	UCT
Z	Iceland
Z	Greenwich
Z	GMT0
Z	GMT
Z	Etc/Zulu
Z	Etc/Universal
Z	Etc/UTC
Z	Etc/UCT
Z	Etc/Greenwich
Z	Etc/GMT0
Z	Etc/GMT-0
Z	Etc/GMT+0
Z	Etc/GMT
Z	Atlantic/St_Helena
Z	Atlantic/Reykjavik
Z	Atlantic/Azores
Z	America/Scoresbysund
Z	America/Danmarkshavn
Z	Africa/Timbuktu
Z	Africa/Sao_Tome
Z	Africa/Ouagadougou
Z	Africa/Nouakchott
Z	Africa/Monrovia
Z	Africa/Lome
Z	Africa/Freetown
Z	Africa/Dakar
Z	Africa/Conakry
Z	Africa/Bissau
Z	Africa/Banjul
Z	Africa/Bamako
Z	Africa/Accra
Z	Africa/Abidjan

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java 8 – Find Longest String in an Arrays or List or Stream ?
Java 8 – Instant with method details and examples