Last Updated : 19 Jun, 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. fixed() method of Clock class returns a clock object and the Clock object returns the same instant. Clock object is returned by calling
Clock.fixed(parameters)simply returns the same instant as specified using parameters. The returned class object is
immutable, thread-safe and Serializable. The main use of this method is in
testing, where the clock needed is fixed in place of the current clock.
Syntax:public static Clock fixed(Instant fixedInstant, ZoneId zone)Parameters:
This method takes two mandatory parameter:
This method returns Clock object that returns the same instant.
Example:Input:: Instance object as parameter : Instant.parse("2018-08-19T16:45:42.00Z"); TimeZone Object as parameter : ZoneId.of("Asia/Calcutta"); Output:: class object: Explanation:: when Clock.fixed(Instant.parse("2018-08-19T16:45:42.00Z") is called, then the fixed() method will return a clock object in return with fixed time zone and instance.
Below programs illustrates fixed() method of java.time.Clock class:
Program 1:Using fixed() when Zone is defined
Java
// Java program to demonstrate
// fixed() method of Clock class
import java.time.*;
// create class
public class fixedMethodDemo {
// Main method
public static void main(String[] args)
{
// create instance of clock class
Instant instant = Instant.parse("2018-08-19T16:02:42.00Z");
// create ZoneId object for Asia/Calcutta zone
ZoneId zoneId = ZoneId.of("Asia/Calcutta");
// call fixed method
Clock clock = Clock.fixed(instant, zoneId);
// print details of clock
System.out.println(clock.toString());
}
}
Output:
FixedClock[2018-08-19T16:02:42Z, Asia/Calcutta]Program 2:
Using fixed() for default Zone
Java
// Java program to demonstrate
// fixed() method of Clock class
import java.time.*;
// create class
public class fixedMethodDemo {
// Main method
public static void main(String[] args)
{
// create instance of clock class
Instant instant = Instant.now();
// create ZoneId for defaultZone which is UTC
ZoneId zoneId = ZoneId.systemDefault();
// call fixed method
Clock clock = Clock.fixed(instant, zoneId);
// print details of clock
System.out.println(clock.toString());
}
}
Output:
FixedClock[2018-08-21T08:10:32.498Z, Etc/UTC]Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#fixed-java.time.Instant-java.time.ZoneId-
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4