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); } } |
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