java.time.Instant Class in Java

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



