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:
- Java 8 – LocalDate with method details and examples
- Java 8 – LocalTime with method details and examples
- Java 8 – LocalDateTime with method details and examples
- Java 8 – ZonedDateTime with method details and examples
- Java 8 – Display all Zones and its Offset using ZoneId and ZoneOffset
- Java 8 – OffsetDateTime with method details and examples
- Java 8 – Instant with method details and examples
- Java 8 – How to display Zones for particular Offset ?
- Java 8 – Clock with method details and examples
- Java 8 – OffsetTime with method details and examples
- Java 8 – Period with method details and examples
- Java 8 – Duration with method details and examples
- Java 8 – How to get current Date and Time ?
- Java 8 – How to sort List by java.util.Date in different ways ?
- Java 8 – How to sort List by java.time.LocalDate in different ways ?
- Java 8 – How to sort List by java.time.LocalDateTime in different ways ?
- Java 8 – How to sort List by java.time.ZonedDateTime in different ways ?
- Java 8 – How to sort List by java.time.OffsetDateTime in different ways ?
References:
- https://docs.oracle.com/javase/7/docs/api/java/util/Date.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Calendar.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Timestamp.html
- https://docs.oracle.com/javase/7/docs/api/java/sql/Time.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDate.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/LocalDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/OffsetDateTime.html
- https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneId.html
- https://docs.oracle.com/javase/8/docs/api/java/time/ZoneOffset.html
- https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html
Happy Coding !!
Happy Learning !!