Home » Java Instant

Java Instant Class

Java Instant class is used to represent the specific moment on the time line. It inherits the Object class and implements the Comparable interface.

Java Instant Class Declaration

Let’s see the declaration of java.time.Instant class.

Methods of Java Instant

Method Description
Temporal adjustInto(Temporal temporal). It is used to adjust the specified temporal object to have this instant.
int get(TemporalField field) It is used to get the value of the specified field from this instant as an int.
boolean isSupported(TemporalField field) It is used to check if the specified field is supported.
Instant minus(TemporalAmount amountToSubtract) It is used to return a copy of this instant with the specified amount subtracted.
static Instant now() It is used to obtain the current instant from the system clock.
static Instant parse(CharSequence text) It is used to obtain an instance of Instant from a text string such as 2007-12-03T10:15:30.00Z.
Instant plus(TemporalAmount amountToAdd) It is used to return a copy of this instant with the specified amount added.
Instant with(TemporalAdjuster adjuster) It is used to return an adjusted copy of this instant.
Instant plus(long amountToAdd, TemporalUnit unit) It returns a copy of this instant with the specified amount added.
OffsetDateTime atOffset(ZoneOffset offset) It combines the instant with an offset to create an OffsetDateTime.
ZonedDateTime atZone(ZoneId zone) It combines the instant with a time-zone to create a ZonedDateTime.
int compareTo(Instant otherInstant) It compares the instant to the specified instant.
boolean equals(Object otherInstant) It checks if the instant is equal to the specified instant.
static Instant from(TemporalAccessor temporal) It obtains an instance of Instant from a temporal object.
int get(TemporalField field) It gets the value of the specified field from this instant as an int.
long getEpochSecond() It gets the number of seconds from the Java epoch of 1970-01-01T00:00:00Z.
long getLong(TemporalField field) It gets the value of the specified field from this instant as a long.
int getNano() It gets the number of nanoseconds, later along the time-line, from the start of the second.
int hashCode() It returns a hash code for this instant.
boolean isAfter(Instant otherInstant) It checks if the instant is after the specified instant.
boolean isBefore(Instant otherInstant) It checks if the instant is before the specified instant.
static Instant ofEpochMilli(long epochMilli) It obtains an instance of Instant using milliseconds from the epoch of 1970-01-01T00:00:00Z.
static Instant ofEpochSecond(long epochSecond) It obtains an instance of Instant using seconds from the epoch of 1970-01-01T00:00:00Z.
Instant truncatedTo(TemporalUnit unit) It returns a copy of the Instant truncated to the specified unit.
long until(Temporal endExclusive, TemporalUnit unit) It calculates the amount of time until another instant in terms of the specified unit.
String toString() A string representation of the instant using ISO-8601 representation.

Java Instant Example: parse()

InstantExample1.java

Test it Now

Output:

2017-02-03T10:37:30Z  

Java Instant Example: now()

InstantExample2.java

Test it Now

Output:

2017-02-03T06:11:01.194Z  

Java Instant Example: minus()

InstantExample3.java

Test it Now

Output:

2016-10-01T11:25:30Z  

Java Instant Example: plus()

InstantExample4.java

Test it Now

Output:

2017-06-08T11:25:30Z  

Java Instant Example: isSupported()

InstantExample5.java

Test it Now

Output:

true  false  

You may also like