Developer Documentation
Java
Updated September 19, 2026

Unix Timestamp and Epoch in Java

Generate and convert Unix timestamps in seconds and milliseconds in Java.

Unix time counts elapsed time from the Unix epoch at 1970-01-01T00:00:00Z. Different ecosystems use different units, so always label whether a value is in seconds, milliseconds, microseconds, or nanoseconds.

Quick Start#

The following example uses the standard Java API or a widely used package for this task.

Quick start#

Java
1import java.time.Instant;23Instant now = Instant.now();4long seconds = now.getEpochSecond();5long milliseconds = now.toEpochMilli();6System.out.println(seconds);7System.out.println(milliseconds);

Common Operations#

This second pattern covers a common production operation or a safer way to handle real input.

Convert back to ISO-8601#

Java
1Instant instant = Instant.ofEpochSecond(epochSeconds);2System.out.println(instant);34Instant fromMs = Instant.ofEpochMilli(epochMilliseconds);

Important Notes#

Use Instant for a moment on the UTC timeline and ZonedDateTime only when you need presentation/business rules in a named timezone.

Best Practices#

  1. Treat external input as untrusted and handle parse/validation errors explicitly.
  2. Keep encoding, units, formats, and library-specific mapping rules explicit at system boundaries.
  3. Add test vectors that cover empty input, Unicode text, malformed input, and representative production data.

References#

Implement Unix Timestamp in other languages: