Java Clock Class | system() Method with Example

Clock Class system() method: Here, we are going to learn about the system() method of Clock Class with its syntax and example.
Submitted by Preeti Jain, on May 13, 2020

Clock Class system() method

  • system() method is available in java.time package.
  • system() method is used to select system clock that is available in the given zone.
  • system() method is a static method, it is accessible with class name and if we try to access the method with class object then we will not get an error.
  • system() method does not throw an exception at the time of representing Clock.

Syntax:

    public static Clock system(ZoneId zo);

Parameter(s):

  • ZoneId zo – represents the zone that is used to translate the instant to date:time.

Return value:

The return type of this method is Clock, it returns the Clock object that represent the current instant system clock in the given time zone.

Example:

// Java program to demonstrate the example 
// of system(ZoneId zo) method of Clock

import java.time.*;

public class SystemOfClock {
    public static void main(String args[]) {
        // Instantiates two ZoneId for Accra and Asmara
        ZoneId zone_1 = ZoneId.of("Africa/Accra");
        ZoneId zone_2 = ZoneId.of("Africa/Asmara");

        // generates a Clock for the given time-zone(zone_1) 
        // that returns the current instant for this Clock cl1
        Clock cl1 = Clock.system(zone_1);

        // generates a Clock for the given time-zone(zone_2) 
        // that returns the current instant for this Clock cl2
        Clock cl2 = Clock.system(zone_2);

        String str1 = cl1.toString();
        String str2 = cl2.toString();

        // Display str1 and str2
        System.out.println("Clock.system(zone_1): " + str1);
        System.out.println("Clock.system(zone_2): " + str2);
    }
}

Output

Clock.system(zone_1): SystemClock[Africa/Accra]
Clock.system(zone_2): SystemClock[Africa/Asmara]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.