Instant & Machine Time
Instant & Machine Time
Most of the java.time API is designed around human time — calendars, time zones, months, and day-of-week. But computers and distributed systems often need something simpler: a single number that unambiguously represents a point on the global timeline. That is exactly what Instant provides.
The Unix Epoch and What "Machine Time" Means
By international convention, time on computers is measured from the Unix epoch: midnight UTC on 1 January 1970 (1970-01-01T00:00:00Z). Every moment in history is expressed as a signed count of seconds (and fractions of seconds) relative to that anchor.
Machine time is timezone-free, locale-free, and calendar-free. There is no concept of "Tuesday" or "March" — just a number on a number line. This makes it perfect for:
- Timestamping log entries and audit records
- Measuring elapsed wall-clock time in benchmarks
- Comparing events across distributed systems in different regions
- Storing and transmitting time in databases and JSON APIs
The Instant Class
java.time.Instant represents a point on the timeline with nanosecond precision. Internally it holds two fields: long epochSecond (seconds since 1970-01-01T00:00:00Z) and int nanos (the nanosecond adjustment within that second, 0–999,999,999).
toString() of every Instant ends in Z (Zulu, the NATO phonetic for UTC). This is the ISO-8601 representation of a UTC timestamp. When you parse or emit timestamps for APIs, that Z signals "this is already in UTC — no local offset".
Creating Instants
Beyond Instant.now() there are several factory methods:
Querying an Instant
toEpochMilli() when integrating with legacy code (JDBC timestamps, Date, external libraries) because millisecond precision is the common denominator. Only reach for getNano() when you genuinely need sub-millisecond granularity.
Machine Time vs Human Time: Why the Distinction Matters
Consider storing a user's appointment. If you store it as an Instant you capture the UTC point-in-time, but you lose the intent: the user said "9 AM in New York" — and if a daylight-saving transition happens before the appointment, 9 AM UTC is no longer 9 AM local time. That is a human-time problem.
Conversely, if you store a distributed log event as a ZonedDateTime in a server's local zone, and that server is in a different region from another server, correlating events across the two logs becomes a painful timezone conversion exercise. That is a problem Instant solves trivially.
The rule of thumb:
- Use
Instantfor: log timestamps, audit trails, inter-service event ordering, measuring durations, storing "when did this happen in absolute terms". - Use
ZonedDateTime/LocalDateTimefor: calendars, scheduling, anything the user sees and reasons about in their local time.
Arithmetic on Instants
Instant supports addition and subtraction via Duration (covered in the next lesson), plus convenience methods for common units:
Instant for elapsed-time benchmarks in performance-critical code. Instant.now() reads the system wall clock, which can jump backwards during NTP corrections. For high-precision benchmarking use System.nanoTime() — it is monotonic (never goes backward) but has no epoch meaning and cannot be converted to a date. Use Instant when you need a real wall-clock timestamp; use System.nanoTime() when you only need a duration.
Converting Between Instant and Human-Readable Types
You will frequently need to convert an Instant into a zoned or local representation for display, or convert back after user input:
Instant and Legacy APIs
Before Java 8 the world used java.util.Date and java.sql.Timestamp. You will encounter these in older codebases, JDBC drivers, and ORM mappings. The bridge is straightforward:
Summary
Instant is the java.time representation of machine time: a timezone-free, calendar-free point on the UTC timeline measured from the Unix epoch. Use it wherever you need an unambiguous global timestamp — logging, event ordering, storage, and inter-service communication. For anything a human reads or schedules in a local timezone, convert to ZonedDateTime. The next lesson introduces Duration and Period, the two classes that represent spans of time.