DevToolsForYou

Unix Timestamp Converter in Java — Code Examples

Unix Timestamp Converter in JavaUse the online tool →

A Unix timestamp is the number of seconds (or milliseconds) since 1 January 1970 00:00:00 UTC. Here is how to get the current timestamp, convert to a human-readable date, and parse date strings back to timestamps in each language.

Java 8+ introduced the java.time package. Use Instant for UTC timestamps and ZonedDateTime for timezone-aware conversion.

Java 8+ (java.time)
import java.time.*;
import java.time.format.DateTimeFormatter;

public class TimestampExample {
    public static void main(String[] args) {
        // Current timestamp
        long nowSec = Instant.now().getEpochSecond();   // seconds
        long nowMs  = System.currentTimeMillis();        // milliseconds

        // Timestamp → Instant (UTC)
        Instant instant = Instant.ofEpochSecond(1700000000);
        System.out.println(instant); // 2023-11-14T22:13:20Z

        // Instant → ZonedDateTime
        ZonedDateTime utc = instant.atZone(ZoneOffset.UTC);
        ZonedDateTime kolkata = instant.atZone(ZoneId.of("Asia/Kolkata"));
        System.out.println(kolkata); // 2023-11-15T03:43:20+05:30[Asia/Kolkata]

        // Format
        String formatted = utc.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z"));
        System.out.println(formatted);

        // String → timestamp
        ZonedDateTime parsed = ZonedDateTime.parse("2023-11-14T22:13:20Z");
        long ts = parsed.toEpochSecond();
        System.out.println(ts); // 1700000000
    }
}
Notes & gotchas
  • Prefer java.time over the legacy java.util.Date and java.util.Calendar — they have serious design flaws.
  • Instant represents a UTC moment; ZonedDateTime adds timezone context for display.
  • System.currentTimeMillis() is faster than Instant.now().toEpochMilli() for high-frequency logging.
Try it in your browser

Need to epoch/unix converter without writing code? The Unix Timestamp Converter runs entirely in your browser — paste your input and get the result instantly. No signup, no install, no data sent to a server.

Open Epoch/Unix Converter
Unix Timestamp Converter in other languages