The Wayback Machine - https://web.archive.org/web/20240707223811/https://www.geeksforgeeks.org/java-time-instant-class-in-java/
Open In App

java.time.Instant Class in Java

Last Updated : 09 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In Java language, the Instant Class is used to represent the specific time instant on the current timeline. The Instant Class extends the Object Class and implements the Comparable interface.

Declaration of the Instant Class

public final class Instant extends Object    
implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable  

Fields of the class:

Field Description
EPOCH Constant for the 1970-01-01T00:00:00Z epoch instant.
MAX The maximum supported Instant, ‘1000000000-12-31T23:59:59.999999999Z’.
MIN The minimum supported Instant, ‘-1000000000-01-01T00:00Z’.

Methods of the class:

Method

Description

adjustInto(Temporal temporal) This method adjusts the specified temporal object to have this instant.
atOffset(ZoneOffset offset) This method combines this instant with an offset to create an OffsetDateTime.
atZone(ZoneId zone) This method combines this instant with a time-zone to create a ZonedDateTime.
compareTo(Instant otherInstant) This method compares this instant to the specified instant.
equals(Object otherInstant) This method checks if this instant is equal to the specified instant.
from(TemporalAccessor temporal) This method obtains an instance of Instant from a temporal object.
get(TemporalField field) This method gets the value of the specified field from this instant as an int.
getEpochSecond() This method gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
getLong(TemporalField field) This method gets the value of the specified field from this instant as along.
getNano() This method gets the number of nanoseconds, later along the time-line, from the start of the second.
hashCode() This method returns a hash code for this instant.
isAfter(Instant otherInstant) This method checks if this instant is after the specified instant.
isBefore(Instant otherInstant) This method checks if this instant is before the specified instant.
isSupported(TemporalField field) This method checks if the specified field is supported.
isSupported(TemporalUnit unit) This method checks if the specified unit is supported.
minus(long amountToSubtract, TemporalUnit unit) This method returns a copy of this instant with the specified amount subtracted.
minus(TemporalAmount amountToSubtract) This method returns a copy of this instant with the specified amount subtracted.
minusMillis(long millisToSubtract) This method returns a copy of this instant with the specified duration in milliseconds subtracted.
minusNanos(long nanosToSubtract) This method returns a copy of this instant with the specified duration in nanoseconds subtracted.
minusSeconds(long secondsToSubtract) This method returns a copy of this instant with the specified duration in seconds subtracted.
now() This method obtains the current instant from the system clock.
now(Clock clock) This method obtains the current instant from the specified clock.
ofEpochMilli(long epochMilli) This method obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
ofEpochSecond(long epochSecond) This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.
ofEpochSecond(long epochSecond, long nanoAdjustment) This method obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z and nanosecond fraction of second.
parse(CharSequence text) This method obtains an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.
plus(long amountToAdd, TemporalUnit unit) This method returns a copy of this instant with the specified amount added.
plus(TemporalAmount amountToAdd) This method returns a copy of this instant with the specified amount added.
plusMillis(long millisToAdd) This method returns a copy of this instant with the specified duration in milliseconds added.
plusNanos(long nanosToAdd) This method returns a copy of this instant with the specified duration in nanoseconds added.
plusSeconds(long secondsToAdd) This method returns a copy of this instant with the specified duration in seconds added.
query(TemporalQuery<R> query) This method queries this instant using the specified query.
range(TemporalField field) This method gets the range of valid values for the specified field.
toEpochMilli() This method converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.
toString() A string representation of this instant using ISO-8601 representation.
truncatedTo(TemporalUnit unit) This method returns a copy of this Instant truncated to the specified unit.
until(Temporal endExclusive, TemporalUnit unit) This method calculates the amount of time until another instant in terms of the specified unit.
with(TemporalAdjuster adjuster) This method returns an adjusted copy of this instant.
with(TemporalField field, long newValue) This method returns a copy of this instant with the specified field set to a new value.

Example:

The following example shows how the different methods associated with the classwork

Java




import java.time.*;
import java.time.temporal.*;
public class GFG {
    public static void main(String[] args)
    {
        // Parsing a string sequence to Instant
        Instant inst1 = Instant.parse("2021-02-09T11:19:42.12Z");
        System.out.println("Parsed instant is " + inst1);
        
        // Using isSupported() method to check whether the
        // specified field is supported or not
        System.out.println(inst1.isSupported(ChronoUnit.DAYS));
        System.out.println(inst1.isSupported(ChronoUnit.YEARS));
        
        // Using Instant.now() to get current instant
        Instant cur = Instant.now();
        System.out.println("Current Instant is " + cur);
        
        // Using minus() method to find instant value after
        // subtraction
        Instant diff = inst1.minus(Duration.ofDays(70));
        System.out.println("Instant after subtraction : "+ diff);
        
        // Using plus() method to find instant value after
        // addition
        Instant sum = inst1.plus(Duration.ofDays(10));
        System.out.println("Instant after addition : "+ sum);
    }
}


Output

Parsed instant is 2021-02-09T11:19:42.120Z
true
false
Current Instant is 2021-03-03T16:27:54.378693Z
Instant after subtraction : 2020-12-01T11:19:42.120Z
Instant after addition : 2021-02-19T11:19:42.120Z


Similar Reads

Instant isSupported() method in Java with Examples
In Instant class, there are two types of isSupported() method depending upon the parameters passed to it. isSupported(TemporalField field) isSupported() method of a Instant class used to Check if the specified field is supported by Instant class or not means using this method we can check if this instant can be queried for the specified field. The
2 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 generates a timestamp for clock object. Here returned In
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 instances are equal, then true is returned if both inst
2 min read
Instant compareTo() method in Java with Examples
The compareTo(Instant otherInstant) method of Instant class used to compare this instance to the instance passed as parameter to check whether both instances are equal. 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 this instance is great
3 min read
Instant atZone() method in Java with Examples
The atZone(ZoneId zone) method of Instant class is used to combine this instant with a time-zone whose ZoneId is given as parameter to create an ZonedDateTime object. This method takes ZoneId as a parameter and combines the time-zone with this instant after operation returns a ZonedDateTime object. If the instant is too large to fit into a zoned da
2 min read
Instant atOffset() method in Java with Examples
The atOffset(ZoneOffset offset) method of Instant class is used to combine this instant with an offset to create an OffsetDateTime object. This method takes ZoneOffset as a parameter to return an OffsetDateTime object and this OffsetDataTime object is formed from this instant at the specified offset from UTC/Greenwich. If the instant is too large t
3 min read
Instant getEpochSecond() method in Java with Examples
The getEpochSecond() method of Instant class is used to return the number of seconds from the Java epoch of 1970-01-01T00:00:00Z. It means the difference between time in seconds of this instance and time represented by "1970-01-01T00:00:00Z" is returned by this method. The epoch second count is a simple incrementing count of seconds where the secon
2 min read
Instant adjustInto() method in Java with Example
The adjustInto(Temporal temporal) method of Instant class adjusts the passed temporal object to have this instant on which this method is applied. Syntax: public Temporal adjustInto(Temporal temporal) Parameters: This method accepts a parameter temporal which is the target temporal object to be adjusted. It should not be null. Return Value: This me
2 min read
Instant getLong() method in Java with Examples
The getLong(TemporalField field) method of Instant class is used to gets the value as a long value from this instant for the specified field passed as parameter. This method queries this instant for the value of the field and the returned value will always be within the valid range of values for the field. When the field is not supported and method
3 min read
Instant getNano() method in Java with Examples
The getNano() method of Instant class is used to return the number of nanoseconds later in the time-line represented in this instant, from the start of the second. The nanosecond-of-second value which measures the total number of nanoseconds from the second value returned by getEpochSecond(). Syntax: public int getNano() Returns: This method return
2 min read
three90RightbarBannerImg