java8 - java.time.ZoneId class tutorials with examples


java.time.ZoneId class

java8 - java.time.ZoneId class tutorials with examples

ZoneId is an abstract class defined in java.time package. This is used to represent timezone id  Asia/Calcutta. This provides rules for creating LocalDateTime and Instant objects. Time in the earth or world is divided into time zones. each part of the region is called timezone. Timezone is an offset or number of hours difference from Standard time zone UTC -  Coordinate Universal Time.

Syntax

public abstract class ZoneId implements Serializable 

Zone Id Object Creation It Provides of() method for creating the specific zone. This method takes String of zone id that gets from the ZoneOffset

 ZoneId zoneId1 = ZoneId.of("Asia/Calcutta");  
ZoneId zoneId2 = ZoneId.of("Z"); // Z is an Zone id for UTC  
ZoneId zoneId3 = ZoneId.of("-8"); // -08:00  
System.out.println(zoneId1);  
System.out.println(zoneId2);  
System.out.println(zoneId3);  
ZoneId zoneId4 = ZoneId.of("A");   
System.out.println(zoneId4);

output is

  
Asia/Calcutta  
Z  
-08:00  
Exception in thread "main" java.time.DateTimeException: Invalid ID for ZoneOffset, invalid format: A  
 at java.base/java.time.ZoneOffset.of(ZoneOffset.java:241)  
 at java.base/java.time.ZoneId.of(ZoneId.java:402)  
 at java.base/java.time.ZoneId.of(ZoneId.java:356)  
 at org.cloudhadoop.LocalDateTime.ConvertToDate.main(ConvertToDate.java:17)  

of() method takes following things,
If the letter is Z, it takes Zone id as UTC, if you pass any letter other than Z, it throws “Exception in thread “main” java.time.DateTimeException: Invalid ID for ZoneOffset".
If it contains + or -, It considered as ZoneOffset as a value.
If it contains a string - Asia/Calcutta, that string is considered as zone id

Example Method Usage

systemDefault() method return current system default timezone - Asia/Calcutta getId() method return the id of Zone id
getDisplayName() method - return display name of the specific timezone using internalization

ZoneId defaultZone= ZoneId.systemDefault();    
System.out.println("Default System: "+defaultZone);  
System.out.println("Default System getId : "+defaultZone.getId());  
System.out.println("Default System Display Name : "+defaultZone.getDisplayName(TextStyle.FULL_STANDALONE, Locale.ENGLISH));  

Output is

  
Default System: Asia/Calcutta  
Default System getId : Asia/Calcutta  
Default System Display Name : India Time  

Methods

Method

Description

adjustInto(Temporal temporal)

adjust the Offset of the caller with the same offset of the parameter object

from(TemporalAccessor temporal)

Return the offset instance from temporal instance

getId()

Return the offset id ie Z

get(TemporalField field)

Return the field from an offset as Int value

getRules()

Return timezone rules

systemDefault()

Return Default timezone id

getLong()

Return the field from an offset as a Long value

of(String offsetId)

It is used to create a ZoneOffset object with id

ofHours(int hours)

It is used to create a ZoneOffset object with offset hours

ofHoursMinutes(int offset Hours,int offsetMinutes)

It is used to create a ZoneOffset object with offset hours and minutes

ofHoursMinutesSeconds(int hours, int minutes, int seconds)

It is used to create a ZoneOffset object with offset hours, minutes and seconds

ofTotalSeconds(int totalSeconds)

It is used to create a ZoneOffset object with total seconds

range(TemporalField field)

It is used to the range of values using Temporal field

How to get available all time zones in java? 

ZoneId class has static getAvailableZoneIds() method - which return available timezone id strings

import java.time.ZoneId;  
import java.util.Set;  
public class ConvertToDate {  
 public static void main(String[] args) {  
     Set availableZones = ZoneId.getAvailableZoneIds();  
     availableZones.stream().forEach(System.out::println);  
 }  
}

The output of the above code is

Asia/Aden  
America/Cuiaba  
Etc/GMT+9  
Etc/GMT+8  
Africa/Nairobi  
America/Marigot  
Asia/Aqtau  
.......  

Similar Posts