Java Reference ยท Date and Time
The Instant class
Reference for java.time.Instant: a point on the UTC timeline, epoch seconds and millis, parsing ISO text, arithmetic, truncation and zone conversion.
java.time.Instant is a single point on the timeline, measured as seconds and nanoseconds since the Unix epoch, 1970-01-01T00:00:00Z. It has no zone and no calendar; its text form is always UTC, with a trailing Z, such as 2025-07-14T07:30:00Z. It is the right type for timestamps: when a record was created, when a token expires, when an event happened.
Because an Instant is the same everywhere in the world, it is ideal for storage, logging and exchange between systems. To show it to people, convert it to a ZonedDateTime in their zone. Arithmetic uses only fixed units (seconds, minutes, hours, days of exactly 24 hours), because calendar units such as months need a zone.
Instant t = Instant.parse("2025-07-14T07:30:00Z"); Instant later = t.plus(Duration.ofHours(2));
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class Instant implements Temporal, TemporalAdjuster, Comparable<Instant>, Serializable
| Member | Description |
|---|---|
Instant.now() / now(Clock) |
Current instant from the system clock or a given clock. |
ofEpochSecond(long) / ofEpochSecond(long, long) |
From seconds (plus nanosecond adjustment) since the epoch. |
ofEpochMilli(long) |
From milliseconds since the epoch. |
parse(CharSequence) |
ISO-8601 instant text such as 2025-07-14T07:30:00Z. |
getEpochSecond(), getNano(), toEpochMilli() |
Raw values. |
plus(...), minus(...), plusSeconds/Millis/Nanos |
Arithmetic with fixed-length units and Duration. |
until(Temporal, TemporalUnit) |
Amount between two instants. |
truncatedTo(TemporalUnit) |
Drop precision. |
atZone(ZoneId), atOffset(ZoneOffset) |
Attach a zone or offset. |
isBefore, isAfter, compareTo, equals |
Comparison. |
| Constants | EPOCH, MIN, MAX. |
Throws: DateTimeParseException for bad text, UnsupportedTemporalTypeException for units such as months or years, ArithmeticException or DateTimeException on overflow.
Usage notes
- UTC only. The
Zin the text means UTC; anInstanthas no concept of a local date. Ask fortoLocalDate()only after converting withatZone. toEpochMilli()loses precision beyond milliseconds and can throwArithmeticExceptionfor extreme values.- Precision of
now(). Java 9 and later usually provide microsecond precision (platform dependent); Java 8 provides milliseconds. Do not compare round-tripped values with a database that stores less precision without truncating first. - Round-trip.
Instant.parse(instant.toString())returns an equal value. - Not for calendar math. "Same time tomorrow" in a region is a
ZonedDateTimeoperation;plus(Duration.ofDays(1))adds exactly 86,400 seconds. - Sorting and comparing are fast and correct across all instants; this is a good key for event ordering.
- JSON and APIs: serialize as ISO text or epoch milliseconds; agree on which with the other side.
- Testing: inject a
Clockand callInstant.now(clock).