Java

Java Clock- getzone

Java-8 and above versions support DateTime API such that it can is possible to return Timezones, time, date, and datetime related information. getZone() is one of the methods available in the DateTime API.

java.time.Clock.getZone()

Import this package from java.time Clock package. getZone() is used to return the TimeZone of the provided Clock or the System Clock.

Based on the clock provided, it returns the Timezone. It doesn’t take any parameters.

Advantages

    1. Get the Timezone Information.
    2. Used to get and process the date-time based on the returned timezone.
    3. Predict the seasonal rainfall and get the weather conditions.

Syntax

 

clock_object.getZone()

 
clock_object represents the clock that has information.

It can is also possible to pass the ZoneId of a particular Zone within the clock_object.

Return

java.time.Clock.getZone() returns the ZoneId.

Example 1:

Let’s create the clock_object with the default zone and return the Timezone.

import java.time.Clock;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object
        Clock clock_object = Clock.systemDefaultZone();
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the TimeZone from the clock_object
        System.out.println("TimeZone Details: "+clock_object.getZone());
         
        }
}

 

Output

 

So the TimeZone is GMT (GreenWich MeanTime).

Explanation:

Line 8: Create Clock object (clock_object)


Line 10: Return the Clock object


Line 13: Return the TimeZone using getZone() method.

Example 2:

Let’s create the clock_object with the timezone-Colombo and return the Timezone.

Here, we have to pass the ZoneId.of(Zone) parameter in the Clock_object.

import java.time.Clock;
import java.time.ZoneId;

public class Main
{
    public static void main(String[] args) {

        // Create the clock object named clock_object with ZoneId - Colombo
        Clock clock_object = Clock.system(ZoneId.of("Asia/Colombo"));
       
        System.out.println("Clock Details: "+clock_object);
 
        // Return the TimeZone from the clock_object
        System.out.println("TimeZone Details: "+clock_object.getZone());
         
        }
}

 

Output

 

So the TimeZone is Asia/Colombo.

Explanation

Line 8: Create the clock object named clock_object with ZoneId – Colombo.


Line 10: Return the Clock object.


Line 13: Return the Timezone using getZone() method.

Example 3:

Let’s create the clock_object with different timezone and return Timezone using the getZone() method.

import java.time.Clock;
import java.time.ZoneId;

public class Main
{
    public static void main(String[] args) {

        //Return the TimeZone from the clock_object with ZoneId - Colombo
        System.out.println("TimeZone-1: "+Clock.system(ZoneId.of("Asia/Colombo")).getZone());
       
        //Return the TimeZone from the clock_object with ZoneId - Canary
        System.out.println("TimeZone-2: "+Clock.system(ZoneId.of("Atlantic/Canary")).getZone());
       
        //Return the TimeZone from the clock_object with ZoneId - Sofia
        System.out.println("TimeZone-3: "+Clock.system(ZoneId.of("Europe/Sofia")).getZone());
       
         //Return the TimeZone from the clock_object with ZoneId - Turkey
        System.out.println("TimeZone-4: "+Clock.system(ZoneId.of("Turkey")).getZone());
       
        //Return the TimeZone from the clock_object with ZoneId - Portugal
        System.out.println("TimeZone-5: "+Clock.system(ZoneId.of("Portugal")).getZone());
         
        }
}

 

Output

 

Explanation

Line 8-21: We directly passed the ZoneId parameter and returned Timezones.

Conclusion

In this Java tutorial, we saw how to return the TimeZone of particular Zones using the getZone() method. This method works only for Java-8 and above versions. If you don’t provide the oneId inside the clock object, then you can create a clock object using systemDefaultZone().

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain