Skip to content
Tutorials Center
My learning

Topics

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.

Java
Instant t = Instant.parse("2025-07-14T07:30:00Z");
Instant later = t.plus(Duration.ofHours(2));

Try it

Java Creating, parsing and epoch values

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.

Java Arithmetic, comparison and zones

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

Java
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 Z in the text means UTC; an Instant has no concept of a local date. Ask for toLocalDate() only after converting with atZone.
  • toEpochMilli() loses precision beyond milliseconds and can throw ArithmeticException for 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 ZonedDateTime operation; 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 Clock and call Instant.now(clock).