Java Snippets

Java Unix Timestamp Snippets

Java examples for current epoch seconds, milliseconds, Instant conversion, timezone-aware formatting, and modern java.time APIs.

Current Unix timestamp (seconds)
import java.time.Instant;

Instant.now().getEpochSecond()
Current Unix timestamp (milliseconds)
System.currentTimeMillis()
// or
Instant.now().toEpochMilli()
Instant from timestamp
import java.time.Instant;

Instant.ofEpochSecond(1700000000)
Instant.ofEpochMilli(1700000000000L)
Formatted string (UTC)
import java.time.*;
import java.time.format.DateTimeFormatter;

Instant.ofEpochSecond(1700000000)
    .atZone(ZoneOffset.UTC)
    .format(DateTimeFormatter.ISO_DATE_TIME)
In a specific timezone
import java.time.*;

Instant.ofEpochSecond(1700000000)
    .atZone(ZoneId.of("America/New_York"))
    .toString()

Java timestamp basics

Modern Java code should prefer java.time.Instant for timestamp work. Instant.now().getEpochSecond() gives Unix seconds and Instant.now().toEpochMilli() gives Unix milliseconds.

Java millis to Unix timestamp

Java code commonly starts with System.currentTimeMillis() — a long of epoch milliseconds. For a standard Unix timestamp in seconds, divide by 1000. For a date, build an Instant. Use the explicit conversions below rather than passing the long through different APIs that may expect either unit.

  • long millis = System.currentTimeMillis(); // 13 digits, epoch ms
  • long secs = System.currentTimeMillis() / 1000L; // millis to Unix timestamp
  • Instant inst = Instant.ofEpochMilli(millis); // currentTimeMillis to date (Instant)
  • String iso = Instant.ofEpochMilli(millis).toString(); // ISO 8601 UTC
  • ZonedDateTime zdt = Instant.ofEpochMilli(millis).atZone(ZoneId.of("UTC"));
  • Use System.nanoTime() only for elapsed-time measurements — it is not epoch-based

Timezone-aware Java formatting

Convert an Instant to a ZonedDateTime with ZoneId when displaying local time. Keep storage and comparisons in Instant or UTC, then format only at the edge of the application.

Java production notes

The java.time API separates instants, local calendar values, and timezone-aware display. Use Instant for the moment that should be stored or compared. Use ZonedDateTime only after selecting a ZoneId for a user, tenant, or report. Avoid legacy Date and Calendar unless an older library requires them.

  • Use Instant.now().getEpochSecond() for Unix seconds
  • Use Instant.now().toEpochMilli() or System.currentTimeMillis() for milliseconds
  • Use Instant.ofEpochSecond(seconds) when receiving standard Unix timestamps
  • Use DateTimeFormatter.ISO_INSTANT for compact UTC API output

FAQ

Should new code use Date or Instant?
Use Instant for timestamp storage and comparisons. Legacy Date may still appear in old libraries, but java.time is clearer for seconds, milliseconds, and timezone-aware formatting.
What is the difference between Instant and ZonedDateTime?
Instant is the exact moment in UTC. ZonedDateTime is that moment displayed with a ZoneId, which is useful for user-facing dates and reports.
How do I verify Java epoch values?
Print Instant.ofEpochSecond(seconds) or Instant.ofEpochMilli(milliseconds) before saving a value. The ISO output should match the UTC instant expected by the receiving system.
How do I convert millis to a Unix timestamp in Java?
Divide System.currentTimeMillis() by 1000L for a long of Unix seconds: long secs = System.currentTimeMillis() / 1000L. Use Instant.ofEpochSecond(secs) to round-trip back into an Instant.
How do I convert currentTimeMillis to a date in Java?
Wrap it in an Instant: Instant.ofEpochMilli(System.currentTimeMillis()). For display, combine with a ZoneId: ZonedDateTime.ofInstant(instant, ZoneId.of("UTC")) or any other IANA zone.