Skip to content
Related Articles

Related Articles

Improve Article

Java 8 Clock getZone() method with Examples

  • Last Updated : 10 May, 2019

Java Clock class is part of Date Time API, java.time.Clock, of Java. The Java Date Time API was added from Java version 8.

getZone() method of Clock class returns the time-zone used to create dates and times of Clock class. Every Clock class needs a Time Zone for obtaining the current instant of time and convert that instance to a date or time. So this method returns the time-zone used for this process.

Syntax:

public abstract ZoneId getZone()

Returns Value: This Method returns the time-zone used to create Clock class.

Example:



Input:: 
a clock class Object e.g Clock.systemUTC()

Output::
TimeZone e.g. Z

Explanation:: 
when Clock.getZone() is called, then it returns a Time Zone used in Class Object

Below programs illustrates getZone() method of java.time.Clock class:

Program 1: Get the TimeZone of the Clock obejct, created with systemDefaultZone, using getZone()




// Java program to demonstrate getZone()
// method of Clock class
  
import java.time.*;
  
// create class
public class getZoneMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create Clock Object
        Clock clock = Clock.systemDefaultZone();
  
        // get TimeZone of Clock object
        // using  getZone() method
        ZoneId zoneid = clock.getZone();
  
        // print details of TimeZone
        System.out.println("ZoneId for class "
                           + clock + " is " + zoneid);
    }
}
Output:
ZoneId for class SystemClock[Etc/UTC] is Etc/UTC

Program 2:Get TimeZone of Clock object, with Zone “Asia/calcutta”, using getZone().




// Java program to demonstrate getZone() 
// method of Clock class
  
import java.time.*;
  
// create class
public class getZoneMethodDemo {
  
    // Main method
    public static void main(String[] args)
    {
  
        // create a Zone Id for Calcutta
        ZoneId zoneId = ZoneId.of("Asia/Calcutta");
  
        // create Clock Object by passing zoneID
        Clock clock = Clock.system(zoneId);
  
        // get TimeZone of Clock object
        // using  getZone() method
        // and save ZoneId as zoneid variable
        ZoneId zoneid = clock.getZone();
  
        // print details of TimeZone
        System.out.println("ZoneId for clock is "
                           + zoneid);
    }
}
Output:
ZoneId for clock is Asia/Calcutta

Reference:
https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#getZone–

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :