Create an instant from seconds
The Instant class represents an instant as seconds since the Epoch and a nanosecond fraction. Use ofEpochSecond for seconds and ofEpochMilli for milliseconds.
import java.time.Instant;
import java.time.Duration;
import java.time.ZoneId;
Instant instant = Instant.ofEpochSecond(1711972800L);
System.out.println(instant); // 2024-04-01T12:00:00Z
System.out.println(instant.toEpochMilli()); // 1711972800000
System.out.println(instant.atZone(ZoneId.of("Europe/Paris")));Compare two timestamps
Compare instants after converting their units. isBefore, isAfter and equals answer different questions from simply displaying local time.
Instant start = Instant.parse("2024-04-01T12:00:00Z");
Instant end = Instant.parse("2024-04-01T14:30:00Z");
System.out.println(start.isBefore(end)); // true
System.out.println(Duration.between(start, end).toSeconds()); // 9000Interoperate with JDBC
The legacy class is java.sql.Timestamp, not java.util.Timestamp. Use Timestamp.from(instant) and timestamp.toInstant() if your JDBC layer requires it. SQL storage and the driver must use a consistent time zone convention.
LocalDateTime represents calendar fields without a time zone. By itself, it is not enough to recover a universal timestamp. For execution durations, prefer a monotonic clock such as System.nanoTime().
References
English version published September 26, 2026.