Java 8 Clock instant() method with Examples
Last Updated :
14 Apr, 2023
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 generates a timestamp for clock object. Here returned Instant is Object of java.time.Instant class which represents a specific moment on the timeline in UTC Zone. This timeline is a count of nanoseconds since the epoch of the first moment of 1970 UTC. Since nowadays most of the business logic, data storage, and data exchange should be in UTC, so using Instant is useful.
Syntax:
public abstract Instant instant()
Return Value: This method returns the current instant of clock object.
Exception: This method throws a DateTimeException if the instant of clock object cannot be obtained.
Example:
Input::
a clock class Object e.g Clock.systemDefaultZone()
Output::
instant e.g. 2018-08-19T20:22:23.366Z
Explanation::
when instant() is called, it returns a current instant of Clock Class Object.
Below programs illustrates instant() method of java.time.Clock class:
Program 1: Get Clock object with systemDefaultZone using instant()
Java
// Java Program to demonstrate
// instant() method of Clock class
import java.time.*;
// create class
public class instantMethodDemo {
// Main method
public static void main(String[] args)
{
// create Clock Object
Clock clock = Clock.systemDefaultZone();
// get Instant Object of Clock
// object using instant() method
Instant instantObj = clock.instant();
// print details of Instant Object
System.out.println("Instant for class " + clock
+ " is " + instantObj);
}
}
Output:Instant for class SystemClock[Etc/UTC] is 2018-08-21T05:31:10.662Z
Program 2: Get Clock object with Zone "Europe/Paris" using instant() To get zonal based date and time, get ZonedDateTime object from instant by using atZone(ZoneId zone) to print date and time of that Zone.
Syntax:
// get ZonedDateTime object from instant object returned by instant() method of Clock class
ZonedDateTime time = Clock.systemDefaultZone().instant().atZone(Clock.getZone());
Code:
Java
// Java Program to demonstrate
// instant() method of Clock class
import java.time.*;
// create class
public class instantMethodDemo {
// Main method
public static void main(String[] args)
{
// create a Zone Id for Europe/Paris
ZoneId zoneId = ZoneId.of("Europe/Paris");
// create Clock Object by passing zoneID
Clock clock = Clock.system(zoneId);
// get Instant Object of Clock
// object using instant() method
Instant instantObj = clock.instant();
// get ZonedDateTime object from
// instantObj to get zoned date time
ZonedDateTime time = instantObj.atZone(clock.getZone());
// print details of Instant Object
System.out.println("Instant for class " + clock
+ " is " + time.toString());
}
}
Output:Instant for class SystemClock[Europe/Paris] is 2018-08-21T07:31:13.525+02:00[Europe/Paris]
Reference: https://docs.oracle.com/javase/8/docs/api/java/time/Clock.html#instant--
Similar Reads
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
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
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 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
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
Instant equals() method in Java with Examples The equals(Object otherInstant) method of Instant class is used to compare this Instant to the Instant object passed as parameter. The comparison between both instances is based on the time-line position of the instants. The value to be returned by this method is determined as follows: if both insta
2 min read
Instant now() Method in Java with Examples In Instant class, there are two types of now() method depending upon the parameters passed to it. now() now() method of a Instant class used to obtain the current instant from the system UTC clock.This method will return instant based on system UTC clock. Syntax: public static Instant now() Paramete
2 min read
Java 8 Clock offset() 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 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 durati
3 min read
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