GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Instant with nanosecond precision | java.evolved

Home / Date/Time / Instant with nanosecond precision
Date/Time Intermediate

Instant with nanosecond precision

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Get timestamps with microsecond or nanosecond precision.

Date & Time
Code Comparison
✕ Java 8
// Millisecond precision only
long millis =
    System.currentTimeMillis();
// 1708012345678
✓ Java 9+
// Microsecond/nanosecond precision
Instant now = Instant.now();
// 2025-02-15T20:12:25.678901234Z
long nanos = now.getNano();
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
🎯

Higher precision

Microsecond/nanosecond vs millisecond timestamps.

📐

Type-safe

Instant carries its precision — no ambiguous longs.

🌐

UTC-based

Instant is always in UTC — no timezone confusion.

Old Approach
Milliseconds
Modern Approach
Nanoseconds
Since JDK
9
Difficulty
Intermediate
JDK Support
Instant with nanosecond precision
Available

Widely available since JDK 9 (Sept 2017)

How it works

Java 9 improved the clock resolution so Instant.now() captures microsecond precision on most platforms (nanosecond on some). The old currentTimeMillis() only gives milliseconds.

Related Documentation
Instant ↗ Clock ↗
Proof
View proof source ↗
Related patterns
Date/Time Intermediate

HexFormat

Java 8
// Pad to 2 digits, uppercase
String hex = String.format(
    "%02X", byteValue);
// Parse hex string
int val = Integer.parseInt(
    "FF", 16);
Java 17+
var hex = HexFormat.of()
    .withUpperCase();
String s = hex.toHexDigits(
    byteValue);
byte[] bytes =
    hex.parseHex("48656C6C6F");
Hover to see modern ➜
JDK 17+ →
Date/Time Beginner

java.time API basics

Pre-Java 8
// Mutable, confusing, zero-indexed months
Calendar cal = Calendar.getInstance();
cal.set(2025, 0, 15); // January = 0!
Date date = cal.getTime();
// not thread-safe
Java 8+
LocalDate date = LocalDate.of(
    2025, Month.JANUARY, 15);
LocalTime time = LocalTime.of(14, 30);
Instant now = Instant.now();
// immutable, thread-safe
Hover to see modern ➜
JDK 8+ →
Date/Time Beginner

Duration and Period

Pre-Java 8
// How many days between two dates?
long diff = date2.getTime()
    - date1.getTime();
long days = diff
    / (1000 * 60 * 60 * 24);
// ignores DST, leap seconds
Java 8+
long days = ChronoUnit.DAYS
    .between(date1, date2);
Period period = Period.between(
    date1, date2);
Duration elapsed = Duration.between(
    time1, time2);
Hover to see modern ➜
JDK 8+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.