Instant.now()

Capture the current moment as a UTC timestamp for logs, records and expiry checks, and make it testable with a Clock.

Signature
public static Instant now()
public static Instant now(Clock clock)
  • Added in Java Java 8

Parameters

ParameterTypeDescription
$clockoptionalClock= Clock.systemUTC()The clock to read from; a fixed or offset clock makes the result predictable in tests.

Return value

InstantThe current instant from the system UTC clock, or from the given clock.

Errors

Does not throw. now(clock) throws NullPointerException if clock is null.

Changelog

  • PHP Java 9The system clock can supply a finer resolution than milliseconds where the operating system allows it (microseconds or nanoseconds, depending on the platform).
  • PHP Java 17Clock implements the new InstantSource interface, which has a single instant() method and suits code that only needs the current instant.

An Instant is a point on the UTC timeline: seconds and nanoseconds since 1970-01-01T00:00:00Z. It has no zone, no calendar and no daylight saving, which is exactly why it is the right type for "when did this happen" in a back-end. Instant.now() reads the current one from the system clock.

Where to use it

Record creation and update times, audit entries, token expiry, cache entry ages and event timestamps should all be Instant. Store them as timestamp with time zone (or the database's UTC timestamp type), send them in JSON as ISO-8601 with a Z, and convert to a user's zone only when you display them.

toString() prints ISO-8601, for example 2026-05-01T12:00:00Z, and Instant.parse reads it back. toEpochMilli() and getEpochSecond() give the numbers many APIs and JWT claims use.

Precision

Since Java 9 the system clock can return more than millisecond precision: microseconds on some platforms, nanoseconds on current Linux JDKs. Databases often store microseconds (PostgreSQL) or milliseconds, so a value you save and read back may not equals() the one you held. Truncate before saving, with truncatedTo(ChronoUnit.MICROS) or MILLIS, when your tests compare round-tripped values.

Make it testable

Calling Instant.now() directly ties your code to the real clock. Inject a Clock (or, since Java 17, an InstantSource) and call Instant.now(clock) or clock.instant(). Tests then pass Clock.fixed(...) and move time with Clock.offset(...) instead of sleeping.

Not a stopwatch

The wall clock can jump when the operating system syncs with NTP, so the difference between two Instant.now() calls can even be negative. To measure elapsed time for metrics or timeouts, use System.nanoTime(), which is monotonic, and wrap the result in Duration.ofNanos.

System.currentTimeMillis() and Instant.now().toEpochMilli() return the same kind of value; prefer the Instant in new code because the type says what the number means.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Setting createdAt and updatedAt on a record before inserting it into the database.
  • Checking whether a password reset token has passed its expiresAt timestamp.
  • Stamping outgoing events or audit log entries with an ISO-8601 UTC time.

Frequently asked questions

Is Instant.now() in UTC?

Yes. An Instant has no zone; it is a point on the UTC timeline and prints with a trailing Z. Convert with atZone(zone) only for display.

What is the difference between Instant.now() and System.currentTimeMillis()?

Both read the wall clock. Instant.now() can carry sub-millisecond precision and is a typed value; currentTimeMillis() is a bare long of milliseconds.

How do I test code that calls Instant.now()?

Inject a Clock and call Instant.now(clock). In tests pass Clock.fixed(instant, ZoneOffset.UTC) and use Clock.offset to move time forward.

More examples

Token expiry tested with a fixed clock

Clock.offset moves time forward without sleeping, so the boundary at exactly 15 minutes is tested precisely.

Example · java
import java.time.*;

record Token(String id, Instant expiresAt) {
    boolean expired(Clock clock) {
        return !Instant.now(clock).isBefore(expiresAt);
    }
}

void main() {
    Clock clock = Clock.fixed(Instant.parse("2026-05-01T12:00:00Z"), ZoneOffset.UTC);
    Token token = new Token("tok-1", Instant.now(clock).plus(Duration.ofMinutes(15)));

    IO.println("expires " + token.expiresAt());
    IO.println("now expired?  " + token.expired(clock));
    IO.println("+14m expired? " + token.expired(Clock.offset(clock, Duration.ofMinutes(14))));
    IO.println("+15m expired? " + token.expired(Clock.offset(clock, Duration.ofMinutes(15))));
}
// Output:
// expires 2026-05-01T12:15:00Z
// now expired?  false
// +14m expired? false
// +15m expired? true

Truncating before saving to a database

Truncate to the precision your database column stores, so a saved and reloaded value still equals the one in memory.

Example · java
import java.time.*;
import java.time.temporal.ChronoUnit;

void main() {
    Instant precise = Instant.parse("2026-05-01T12:00:00.123456789Z");
    IO.println("nanos:  " + precise);
    IO.println("micros: " + precise.truncatedTo(ChronoUnit.MICROS));
    IO.println("millis: " + precise.truncatedTo(ChronoUnit.MILLIS));
    IO.println("seconds:" + " " + precise.truncatedTo(ChronoUnit.SECONDS));
}
// Output:
// nanos:  2026-05-01T12:00:00.123456789Z
// micros: 2026-05-01T12:00:00.123456Z
// millis: 2026-05-01T12:00:00.123Z
// seconds: 2026-05-01T12:00:00Z

Converting to and from epoch numbers

JWT claims such as exp use epoch seconds, while JavaScript clients and many logs use epoch milliseconds. Converting through Instant keeps the unit explicit.

Example · java
import java.time.*;

void main() {
    Instant fromMillis = Instant.ofEpochMilli(1_767_225_600_000L);
    IO.println(fromMillis);
    IO.println(fromMillis.getEpochSecond());

    Instant fromSeconds = Instant.ofEpochSecond(1_767_225_600L, 500_000_000);
    IO.println(fromSeconds);
    IO.println(fromSeconds.toEpochMilli());
}
// Output:
// 2026-01-01T00:00:00Z
// 1767225600
// 2026-01-01T00:00:00.500Z
// 1767225600500

Discussion

  • Be the first to comment on this lesson.