Java provides excellent support for Unix timestamps. For modern Java applications (Java 8+), the java.time package (especially Instant) is recommended over the legacy Date and SimpleDateFormat classes. This guide covers both modern and legacy approaches.
How to get the current epoch time
long epoch = System.currentTimeMillis()/1000;Returns epoch in seconds.
Convert from human-readable date to epoch
long epoch = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").parse("01/01/1970 01:00:00").getTime() / 1000;Timestamp in seconds, remove '/1000' for milliseconds.
Convert from epoch to human-readable date
String date = new java.text.SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(new java.util.Date (epoch*1000));Epoch in seconds, remove '*1000' for milliseconds.
Modern Java 8+ Examples (Recommended)
Using java.time.Instant
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
// Get current Unix timestamp in seconds
long epochSeconds = Instant.now().getEpochSecond();
System.out.println(epochSeconds);
// Get current Unix timestamp in milliseconds
long epochMillis = Instant.now().toEpochMilli();
System.out.println(epochMillis);
// Convert timestamp to Instant
Instant instant = Instant.ofEpochSecond(1609459200);
System.out.println(instant);
// Convert Instant to LocalDateTime
LocalDateTime dateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC);
System.out.println(dateTime);The Instant class provides a modern, immutable way to work with Unix timestamps.
Converting Date String to Epoch
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.ZoneOffset;
// Parse date string and convert to epoch
String dateString = "2024-01-01 12:00:00";
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime dateTime = LocalDateTime.parse(dateString, formatter);
long epochSeconds = dateTime.toEpochSecond(ZoneOffset.UTC);
System.out.println(epochSeconds);Use DateTimeFormatter for parsing date strings with the modern API.
Using System.currentTimeMillis()
// Get current timestamp in milliseconds
long currentMillis = System.currentTimeMillis();
System.out.println(currentMillis);
// Convert to seconds
long currentSeconds = currentMillis / 1000;
System.out.println(currentSeconds);
// Convert milliseconds to Instant
Instant instant = Instant.ofEpochMilli(currentMillis);
System.out.println(instant);System.currentTimeMillis() is still widely used and provides millisecond precision.
About Java Unix Timestamps
Java provides excellent support for Unix timestamps through the modern java.time package (Java 8+) and the legacy System.currentTimeMillis() method. For modern Java applications, the java.time.Instant class is recommended for working with Unix timestamps. This guide covers both modern and legacy approaches, helping you choose the right method for your Java version and requirements.
Related guides: Check out our guides for other programming languages: JavaScript, Python, PHP, and more. For timestamp conversion tools, visit our Tools page.
Frequently Asked Questions
How do I get the current Unix timestamp in Java?
For Java 8+, use Instant.now().getEpochSecond() for seconds or Instant.now().toEpochMilli() for milliseconds. For older Java versions, use System.currentTimeMillis() / 1000 for seconds.
How do I convert a Unix timestamp to a date in Java?
Use Instant.ofEpochSecond(seconds) or Instant.ofEpochMilli(milliseconds) to create an Instant, then convert to LocalDateTime or ZonedDateTime as needed. For Java 8+, the java.time package is recommended.
Should I use java.time or the legacy Date class?
For Java 8+, use the java.time package (Instant, LocalDateTime, ZonedDateTime) as it is more modern, thread-safe, and easier to use. The legacy Date and SimpleDateFormat classes should be avoided in new code.