Home »
Java »
Java Reference »
Java Clock Class
Java Clock Class | fixed() Method with Example
Clock Class fixed() method: Here, we are going to learn about the fixed() method of Clock Class with its syntax and example.
Submitted by Preeti Jain, on May 13, 2020
Clock Class fixed() method
- fixed() method is available in java.time package.
- fixed() method is used to represent this Clock as a fixed instant (i.e. it returns the same instant that is similar to this Clock).
- fixed() method is a static method, it is accessible with the class name and if we try to access the method with the class object then we will not get an error.
- fixed() method does not throw an exception at the time of representing Clock.
Syntax:
public static Clock fixed(Instant f_in, ZoneId zo);
Parameter(s):
- Instant f_in – represents the fixed instant to use as this Clock.
- ZoneId zo – represents the time zone that is used to convert the fixed instant to date: time.
Return value:
The return type of this method is Clock, it returns the Clock object that have the same instant values.
Example:
// Java program to demonstrate the example
// of fixed(Instant f_in, ZoneId zo) method of Clock
import java.time.*;
public class FixedOfClock {
public static void main(String args[]) {
// Creating a fixed Instant
Instant in = Instant.ofEpochSecond(2500);
// Instantiates two ZoneId for Accra
// and Asmara
ZoneId zone_1 = ZoneId.of("Africa/Accra");
ZoneId zone_2 = ZoneId.of("Africa/Asmara");
// generates a Clock that returns the same
// instant for the given zone zone_1
Clock cl_fixed = Clock.fixed( in , zone_1);
// Display cl_fixed instant
System.out.println("Clock.fixed(in,zone_1): " + cl_fixed.toString());
// generates a Clock that returns the same
// instant for the given zone zone_2
cl_fixed = Clock.fixed( in , zone_2);
// Display cl_fixed instant
System.out.println("Clock.fixed(in,zone_2): " + cl_fixed.toString());
}
}
Output
Clock.fixed(in,zone_1): FixedClock[1970-01-01T00:41:40Z,Africa/Accra]
Clock.fixed(in,zone_2): FixedClock[1970-01-01T00:41:40Z,Africa/Asmara]