Open In App

java.time.ZoneId Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

A ZoneId is used to identify the rules that used to convert between a LocalDateTime and an Instant of time. The actual rules, describing when and the way the offset changes, are defined by ZoneRules. This class is just an ID wont to obtain the underlying rules. The given approach has opted because the government defines the rules and alters frequently, whereas the ID is stable.

There are two distinct types of ID:

  1. Fixed offsets is a fully resolved offset from UTC/Greenwich, that uses the same offset for all local date-times
  2. Geographical regions are a neighborhood where a selected set of rules for locating the offset from UTC/Greenwich apply

Declaration of java.time.ZoneId class

public abstract class ZoneId extends Object implements Serializable  

A zone map overrides to enable the short time-zone names to be used. The use of short zone IDs has been deprecated in java.util.TimeZone. This map allows the IDs to still be used via the of(String, Map) factory method. This map contains a mapping of the IDs that’s in line with TZDB 2005r and later, where ‘EST’, ‘MST’ and ‘HST’ map to IDs that don’t include daylight savings.

This maps as follows:

EST

05:00

HST 

10:00

MST 

07:00

ACT 

Australia/Darwin

AET 

Australia/Sydney

AGT 

America/Argentina/Buenos_Aires

ART

Africa/Cairo

AST 

America/Anchorage

BET 

America/Sao_Paulo

BST 

Asia/Dhaka

CAT 

Africa/Harare

CNT 

America/St_Johns

CST 

America/Chicago

CTT 

Asia/Shanghai

EAT 

Africa/Addis_Ababa

ECT 

Europe/Paris

IET 

America/Indiana/Indianapolis

IST 

Asia/Kolkata

JST 

Asia/Tokyo

MIT 

Pacific/Apia

NET 

Asia/Yerevan

NST 

Pacific/Auckland

PLT 

Asia/Karachi

PNT

America/Phoenix

PRT 

America/Puerto_Rico

PST 

America/Los_Angeles

SST 

Pacific/Guadalcanal
VST  Asia/Ho_Chi_Minh

Note: The map is unmodifiable.

Methods of ZoneId class:

Methods Description
equals(Object obj) This method checks if this time-zone ID is equal to another time-zone ID.
from(TemporalAccessor temporal) This method obtains an instance of ZoneId from a temporal object.
getAvailableZoneIds() This method gets the set of available zone IDs.
getDisplayName(TextStyle style, Locale locale) This method gets the textual representation of the zone, such as ‘British Time’ or ‘+02:00’.
getId() This method gets the unique time-zone ID.
getRules() This method gets the time-zone rules for this ID allowing calculations to be performed.
hashCode() A hash code for this time-zone ID.
normalized() This method normalizes the time-zone ID, returning a ZoneOffset where possible.
of(String zoneId) This method obtains an instance of ZoneId from an ID ensuring that the ID is valid and available for use.
of(String zoneId, Map<String,String> aliasMap) This method obtains an instance of ZoneId using its ID using a map of aliases to supplement the standard zone IDs.
ofOffset(String prefix, ZoneOffset offset) This method obtains an instance of ZoneId wrapping an offset.
systemDefault() This method gets the system default time-zone.
toString() This method outputs this zone as a String, using the ID.

Below is an example of the implementation of some methods:

Java




// java.time.ZoneId Class in Java with example
import java.time.*;
public class GFG {
    public static void main(String[] args)
    {
  
        // Setting Zone1
        ZoneId zoneid1 = ZoneId.of("Asia/Kolkata");
        // Setting Zone2
        ZoneId zoneid2 = ZoneId.of("Europe/London");
        LocalTime time1 = LocalTime.now(zoneid1);
        LocalTime time2 = LocalTime.now(zoneid2);
        System.out.println(time1);
        System.out.println(time2);
  
        // Checking if the time of zone1
        // comes before time of second zone
        System.out.println(time1.isBefore(time2));
    }
}


Output

22:34:11.044312
17:04:11.044385
false


Last Updated : 03 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Similar Reads