⏱️ TimestampEasy

Home → Unix Timestamp in Java

Unix Timestamp in Java

Java’s modern java.time API is well suited to Unix timestamps because Instant represents a point on the UTC timeline while providing direct epoch conversion methods.

Open TimestampEasy Converter

Working example

System.currentTimeMillis() returns epoch milliseconds. Instant exposes both epoch seconds and epoch milliseconds. Prefer java.time types over older date/time APIs when writing new code.

import java.time.Instant; Instant now = Instant.now(); long seconds = now.getEpochSecond(); long millis = now.toEpochMilli(); Instant again = Instant.ofEpochSecond(seconds);

Unit and timezone checks

In Java, Instant supplies the exact timeline context, while ZoneId and ZonedDateTime handle human-facing regional time. Unix conversion should therefore happen around Instant rather than around an unqualified local date/time.

Common mistakes in Java

When to use Unix timestamps

Java timestamp handling is common in REST services, message queues, database entities, and scheduled jobs where a precise instant needs to cross service boundaries.

Related Timestamp Tools

Unix Timestamps in Java: Instant Is the Key Type

Java's Instant represents a moment on the UTC timeline and includes direct epoch conversion methods. This makes it a strong choice for APIs, logs, database boundaries, and services that need to compare instants without accidentally mixing local timezone rules into the underlying value.

Seconds and milliseconds

Use Instant.now().getEpochSecond() for Unix seconds and Instant.now().toEpochMilli() for milliseconds. To restore an instant, use Instant.ofEpochSecond(value) or Instant.ofEpochMilli(value). When a user needs a local display, convert the Instant to a ZonedDateTime using the desired ZoneId rather than adding a fixed offset yourself.

Example code

Instant now = Instant.now(); long seconds = now.getEpochSecond(); long millis = now.toEpochMilli(); Instant restored = Instant.ofEpochSecond(seconds); System.out.println(restored);

Quick reference

Practical checks

Frequently Asked Questions: Practical Details

What is the difference between Instant and LocalDateTime?

Instant identifies a moment on the UTC timeline; LocalDateTime is a calendar date/time without a timezone or offset.

How do I convert Java epoch milliseconds?

Use Instant.ofEpochMilli() to restore the instant and toEpochMilli() to serialize it.

Why use ZoneId instead of a fixed offset?

ZoneId can apply the relevant daylight-saving and historical rules for a named region.

Frequently Asked Questions

How does Java represent the current Unix time?

Use the Instant facilities shown on this page and confirm the returned unit in the language documentation.

How should Java code choose epoch-second versus epoch-millisecond?

Use epoch seconds for second-based protocols and epoch milliseconds when the protocol needs finer precision. Let the serialization contract determine the choice.

When should Java apply a regional ZoneId?

For Java services, compare Instant values on the UTC timeline and apply ZoneId only when rendering a regional calendar value.