BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java ZoneId class explained with examples

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

ZoneId class in java represents time-zone such as ‘Asia/Kolkata’. There are two main types of Zone Ids: ZoneOffset Ids that consists of ‘Z’ and start with ‘+’ or ‘-‘. The other type of Ids are offset style Ids such as ‘GMT+2’ or ‘UTC+01:00’. ZoneId class provides a way to convert between Instant and LocalDateTime.

java ZoneId class:

public abstract class ZoneId extends Object
implements Serializable

Java ZoneId – Method Summary

MethodDescription
boolean equals(Object obj)Checks if this time-zone id is equal to the other time-zone id.
static Set<String> getAvailableZoneIds()Gets the Set of available Zone Ids.
abstract String getId()It is used to obtain the unique zone id.
abstract ZoneRules getRules()It is used to get the time-zone rules applicable to this zone id. These rules define the calculations that can be performed on this time-zone.
int hashCode()This method returns the hash code for this time-zone id.
static ZoneId systemDefault()This method returns the Zone id of system default time-zone.
static ZoneId from(TemporalAccessor temporal)It obtains an instance of ZoneId from a temporal object.
ZoneId normalized()This method normalizes this zone id and returns a ZoneOffset.
String toString()This method can be used on a ZoneId to get the zone info as a String.
static ZoneId ofOffset(String prefix, ZoneOffset offset)Obtains a Zone Id that has the specified offset.
String getDisplayName(TextStyle style, Locale locale)Returns the Zone information in String form such as ‘India Time’ or ‘+02:00’.

Java ZoneId – of() and now() methods example

Here we are using of() method to obtain an instance of ZoneId by providing the Id information. The now() method is used to obtain the local date and local time from the zone id.

import java.time.*;
public class JavaExample {
  public static void main(String... args) {
    //Obtaining Zone id
    ZoneId zid = ZoneId.of("Asia/Kolkata");
    //Obtaining local time from the zone id
    LocalTime time = LocalTime.now(zid);
    System.out.println(time);
    //Obtaining local date from the zone id
    LocalDate date = LocalDate.now(zid);
    System.out.println(date);
  }
}  

Output:

07:57:37.322601
2022-06-13

Java ZoneId – systemDefault() and getDisplayName() methods example

Method systemDefault() returns the default time zone id and method getDisplayName() is used to obtain the textual representation of the zone id.

import java.util.Locale;
import java.time.ZoneId;
import java.time.format.TextStyle;
public class JavaExample {
  public static void main(String[] args) {
    //getting system default zone id
    ZoneId zid = ZoneId.systemDefault();
    System.out.println(zid);

    //Displaying textual information of zone
    System.out.println(zid.getDisplayName(TextStyle.FULL, Locale.ROOT));
  }
}  

Output:

Asia/Kolkata
India Time

Java ZoneId – getId() and equals() method example

In this example, you will learn the use of two methods of ZoneId class that are getId() and equals(). The getId() method returns the zone id information as a String and equals() compares the two zone ids.

import java.time.ZoneId;
public class JavaExample {
  public static void main(String[] args) {
    //Getting default time zone id
    ZoneId zid = ZoneId.systemDefault();
    String timezone = zid.getId();
    System.out.println("Default time-zone id: "+timezone);

    //Getting the zone id of India
    ZoneId zid2 = ZoneId.of("Asia/Kolkata");
    //Is zid equal to zid2?
    System.out.println(zid.equals(zid2));
  }
}

Output:

Default time-zone id: Asia/Kolkata
true

Reference:

Javadoc

❮ Java Tutorial

Top Related Articles:

  1. Java LocalDateTime
  2. Java String Compare
  3. Java Clock class explained with examples
  4. Java LocalDate – atStartOfDay() method example
  5. How to Parse Date in Desired format – Java Date

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap