Java Instant Class Methods

The java.time.Instant class represents a moment on the timeline in UTC with nanosecond precision.

Java Instant Methods

The table below contains common methods of the Java Instant class, each with a link to a detailed explanation, examples, and real-world uses. Click on the method names to learn more about how to use them effectively in your applications.

Method Description
compareTo() Compares this instant to the specified instant.
equals() Checks if this instant is equal to the specified instant.
from() Obtains an instance of Instant from a temporal object.
get() Gets the value of the specified field from this instant as an int.
getNano() Gets the number of nanoseconds, from 0 to 999,999,999.
getEpochSecond() Gets the number of seconds from the epoch of 1970-01-01T00:00:00Z.
isAfter() Checks if this instant is after the specified instant.
isBefore() Checks if this instant is before the specified instant.
minus() Returns a copy of this instant with the specified duration subtracted.
minusMillis() Returns a copy of this instant with the specified number of milliseconds subtracted.
minusSeconds() Returns a copy of this instant with the specified number of seconds subtracted.
now() Obtains the current instant from the system clock.
parse() Obtains an instance of Instant from a text string such as 2020-10-10T10:15:30.00Z.
plus() Returns a copy of this instant with the specified duration added.
plus() (alternative) Returns a copy of this instant with the specified amount of time added.
plusMillis() Returns a copy of this instant with the specified number of milliseconds added.
plusSeconds() Returns a copy of this instant with the specified number of seconds added.
toEpochMilli() Converts this instant to the number of milliseconds from the epoch of 1970-01-01T00:00:00Z.

In conclusion, the java.time.Instant class provides various methods to work with timestamps, allowing operations such as comparing, adding, subtracting, and converting to different units. For more detailed information, refer to the official Java Documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top