The Instant class in Java is used to represent a specific moment on the time-line. It models a single instantaneous point in time in UTC, independent of time zones. The Instant class is mainly used for machine-based time, such as timestamps, logging events, and measuring time differences.
The Instant class is part of the java.time package, which was introduced in Java 8.
Declaration of the Instant Class
public final class Instant extends Object
implements Temporal, TemporalAdjuster,
Comparable<Instant>, Serializable
Epoch Reference
The Instant class uses the epoch time as its reference point, which is:
1970-01-01T00:00:00Z
Fields of the Class
Field | Description |
|---|---|
EPOCH | Constant representing the epoch instant |
MIN | Minimum supported instant |
MAX | Maximum supported instant |
Methods of the Class
1. Creation Methods
Method | Description |
|---|---|
now() | Obtains the current instant from the system clock |
parse(CharSequence text) | Obtains an instant from an ISO-8601 formatted string |
ofEpochSecond(long epochSecond) | Creates an instant using seconds from the epoch |
ofEpochMilli(long epochMilli) | Creates an instant using milliseconds from the epoch |
2. Time Manipulation Methods
Method | Description |
|---|---|
plus(Duration duration) | Returns a copy of this instant with the specified duration added |
minus(Duration duration) | Returns a copy of this instant with the specified duration subtracted |
plusSeconds(long seconds) | Adds seconds to the instant |
minusMillis(long millis) | Subtracts milliseconds from the instant |
3. Comparison Methods
Method | Description |
|---|---|
compareTo(Instant other) | Compares this instant with another |
isAfter(Instant other) | Checks if this instant is after the specified instant |
isBefore(Instant other) | Checks if this instant is before the specified instant |
equals(Object other) | Checks if two instants are equal |
4. Conversion Methods
Method | Description |
|---|---|
getEpochSecond() | Returns the number of seconds from the epoch |
toEpochMilli() | Converts this instant to milliseconds from the epoch |
atZone(ZoneId zone) | Combines this instant with a time zone |
atOffset(ZoneOffset offset) | Combines this instant with an offset |
Example: The following example demonstrates the use of some commonly used methods of the Instant class.
import java.time.*;
class GFG {
public static void main(String[] args) {
Instant inst1 = Instant.parse("2021-02-09T11:19:42.12Z");
System.out.println("Parsed Instant: " + inst1);
Instant current = Instant.now();
System.out.println("Current Instant: " + current);
Instant added = inst1.plus(Duration.ofDays(10));
System.out.println("Instant after addition: " + added);
Instant subtracted = inst1.minus(Duration.ofDays(5));
System.out.println("Instant after subtraction: " + subtracted);
}
}
Output
Parsed Instant: 2021-02-09T11:19:42.120Z Current Instant: 2025-12-23T10:00:08.966410953Z Instant after addition: 2021-02-19T11:19:42.120Z Instant after subtraction: 2021-02-04T11:19:42.120Z
Explanation:
- Instant.parse() converts an ISO-8601 formatted string into an Instant
- Instant.now() returns the current time in UTC
- plus() and minus() methods are used to perform time arithmetic
- The output is always represented in UTC format.
Note:
- Instant always represents time in UTC and does not store any time-zone.
- It is immutable and thread-safe, so it works safely in multi-threaded programs.
- Best used for timestamps and system-level tasks like logging.
- For human-readable date and time, use LocalDateTime or ZonedDateTime.