Java 8 Clock offset() method with Examples
Last Updated :
10 Dec, 2018
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.
The offset() method is a static method of Clock class which returns a clock with instant equal to the sum of the instants of clock passed as parameter and
specific Offset duration. If the duration added is positive, then the returned clock represents the instant of the clock later by the specified duration from base clock. If the duration added is negative, then the returned clock is earlier than the date and time of the base clock. A duration of Zero does nothing to the base clock and returns the base clock.
If the base clock is immutable, thread-safe and Serializable then returned clock is also immutable, thread-safe and Serializable.
Syntax:
public static Clock offset(Clock baseClock, Duration offsetDuration)
Parameters: This method accepts two mandatory parameters:
- baseclock - a clock for adding duration. It cannot be a null value.
- offsetDuration - duration to add with baseClock. It cannot be a null value as well.
Return Value: This method returns a clock with instant equal to the sum of instants of clock passed as parameter and Specific Offset duration.
Below programs illustrates offset(Clock baseClock, Duration offsetDuration) method of java.time.Clock class:
Program 1: When offset is passed as hours.
Java
// Java program to demonstrate offset()
// method of Clock class
import java.time.*;
// create class
public class offsetMethodDemo {
// Main method
public static void main(String[] args)
{
// base Clock with default zone
Clock realClock = Clock.systemDefaultZone();
// print current time
System.out.println("Real clock instant is "
+ realClock.instant());
// Creating another clock with offset 0
Clock clock = Clock.offset(realClock, Duration.ZERO);
// print new clock
System.out.println("New clock instant"
+ " with Duration = 0 is "
+ clock.instant());
// Creating the clock with 24 hours positive offset
clock = Clock.offset(realClock, Duration.ofHours(24));
// print new clock
System.out.println("New clock instant"
+ " with Duration = 24hours is "
+ clock.instant());
// Creating the clock with 24 hours negative offset
clock = Clock.offset(realClock, Duration.ofHours(-24));
// print new clock
System.out.println("New clock instant"
+ " with Duration = -24hours is "
+ clock.instant());
}
}
Output:
Real clock instant is 2018-08-21T09:43:13.519Z
New clock instant with Duration = 0 is 2018-08-21T09:43:13.785Z
New clock instant with Duration = 24hours is 2018-08-22T09:43:13.785Z
New clock instant with Duration = -24hours is 2018-08-20T09:43:13.785Z
Program 2: When offset is passed as seconds and minutes.
Java
// Java program to demonstrate offset()
// method of Clock class
import java.time.*;
// create class
public class offsetMethodDemo {
// Main method
public static void main(String[] args)
{
// create a Zone Id for Europe/Paris
ZoneId zoneId = ZoneId.of("Europe/Paris");
// base Clock with default zone
Clock realClock = Clock.system(zoneId);
// print current time
System.out.println("Real clock instant is "
+ realClock.instant());
// Creating the clock with 50 seconds positive offset
Clock clock = Clock.offset(realClock, Duration.ofSeconds(50));
// print new clock
System.out.println("Time after 50 second later"
+ " than real Clock is " + clock.instant());
// Creating the clock with 30 minutes positive offset
clock = Clock.offset(realClock, Duration.ofMinutes(30));
// print new clock
System.out.println("Time after 30 minutes later"
+ " than real Clock is " + clock.instant());
}
}
Output:
Real clock instant is 2018-08-21T09:43:18.921Z
Time after 50 second later than real Clock is 2018-08-21T09:44:08.969Z
Time after 30 minutes later than real Clock is 2018-08-21T10:13:18.969Z
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#offset-java.time.Clock-java.time.Duration-
Similar Reads
Java 8 Clock millis() Method with Examples 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.The millis() method of Clock class returns the current instant of the clock in milliseconds. A millisecond instant is measured from 1970-01-01T00:00Z (UTC) to the current time. T
2 min read
Clock tick() Method in Java with Examples tick(Clock baseClock, Duration tickDuration) method of java.time.Clock is a static method of Clock class that returns a clock that returns instants from the base clock rounded to the nearest occurrence of the specified duration in the parameter. The specified base clock duration must be positive, ne
4 min read
Java 8 Clock fixed() method with Examples 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 th
2 min read
Clock system() Method in Java with Examples java.time.Clock.system(ZoneId zone) method is a static method of Clock class which returns a clock that returns the current instant of the clock using best available system clock with ZoneID of the returned clock is set to the ZoneID passed. This method can use System.currentTimeMillis(), or other h
3 min read
Java 8 Clock instant() method with Examples 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. instant() method of Clock class returns a current instant of Clock object as Instant Class Object. Instant generates a timestamp to represent machine time. So this method genera
3 min read
Java 8 Clock getZone() method with Examples 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
2 min read
Java 8 Clock hashCode() method with Examples 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. hashCode() method of Clock class returns the hash code for this Clock Object. Clocks object overrides this method based on their state. If clock object is not overridden, the be
2 min read
OffsetTime compareTo() method in Java with examples The compareTo() method of OffsetTime class in Java compares this time to another time and returns zero if they are equal or a positive or negative integer depending on the comparison result. Syntax: public int compareTo(OffsetTime other) Parameter: This method accepts a single mandatory parameter ot
2 min read
OffsetTime equals() method in Java with examples The equals() method of OffsetTime class in Java checks if this time is equal to another time. returns true if they are equal or false if they are not. Syntax: public boolean equals(Object obj) Parameter: This method accepts a single mandatory parameter obj which specifies the other time which will b
2 min read