CKTIS / Java
time.Instant: practical reference
time.Instant: practical reference: Instant models a point on the UTC time-line as epoch seconds plus nanoseconds.
Version: Java SE 21 (JLS and Java SE API); examples verified on the repository Java runtime
Syntax
java.time.InstantInstant models a point on the UTC time-line as epoch seconds plus nanoseconds.
Use Instant for a machine time-line timestamp and convert it through an explicit ZoneId only when a civil date or local time is needed.
Instant has no time zone or calendar fields. System time can jump, so subtracting two Instant.now readings is not a monotonic performance measurement.
The nanosecond part is normalized from 0 to 999,999,999 even before the epoch. toEpochMilli loses sub-millisecond precision and can overflow near the supported range.
The example exercises the documented element in a small deterministic program and compares exact standard output.
Examples
public class Main {
public static void main(String[] args) throws Exception {
var instant = java.time.Instant.ofEpochSecond(1, 500_000_000);
System.out.println(instant);
}
}
Result
1970-01-01T00:00:01.500Z
The example exercises the documented element in a small deterministic program and compares exact standard output.