Skip to main content

How to Get the Current Time in Java with Instant.now

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

Instant.now() captures the current moment as a point on the UTC timeline, with no time zone involved. That makes it the right type for timestamps you store, compare or send to other systems.

Syntax​

Syntax
import java.time.Instant;

Instant <now> = Instant.now();
PartWhat it does
InstantA single moment on the timeline, in UTC.
Instant.now()Reads the current moment from the system clock.
<now>The moment; printing it gives an ISO 8601 text ending in Z, for UTC.

The JDK 25 documentation for Instant.now: "Obtains the current instant from the system clock. This will query the system UTC clock to obtain the current instant."

The Example​

The example prints the current instant, then runs again with the computer's time zone set to New York, to show the result stays in UTC.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which imports java.time.Instant, sets now to Instant.now() and prints it.

Main.java
import java.time.Instant;

public class Main {
public static void main(String[] args) {
Instant now = Instant.now();
System.out.println(now);
}
}

Step 2: Run It in Two Time Zones​

The second command sets the TZ variable, which tells programs on macOS and Linux which time zone to use, for that one run:

Terminal
java Main.java
TZ=America/New_York java Main.java
Output
2026-11-26T06:34:09.969248Z
2026-11-26T06:34:10.650024Z

A zsh terminal where java Main.java prints 2026-11-26T06:34:09.969248Z, and TZ=America/New_York java Main.java prints 2026-11-26T06:34:10.650024Z.

Both runs printed UTC time, a second apart; New York's time zone didn't change the value. Your times will differ. The TZ=... prefix works in zsh and bash; in PowerShell, run java Main.java on its own.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting Instant to show local timeIt always prints UTC, with a ZConvert it with atZone(ZoneId.systemDefault()) for display
Using LocalDateTime.now() for timestampsThe value has no time zone, so other systems can't tell when it happenedUse Instant.now()
Comparing instants with ==Compares objects, not momentsUse isBefore, isAfter or equals

Troubleshooting​

SymptomLikely causeFix
The printed hour differs from your clockInstant prints UTCAdd your offset, or convert with atZone
cannot find symbol for InstantThe import is missingAdd import java.time.Instant;

FAQ​

How precise is Instant.now()?​

It can hold nanoseconds; on this computer it printed six digits after the seconds, microseconds.

How do I get Unix seconds from it?​

now.getEpochSecond() returns the seconds since 1970-01-01T00:00:00Z.

Conclusion​

Instant.now() returns the current moment in UTC, the same on every computer whatever its time zone. Store and compare Instants, and convert to a time zone only when showing them to people.

Additional Resources​

  • Instant, in the JDK 25 API documentation.