Unix Timestamp Converter in Java — Code Examples
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.
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
}
}- 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.
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 JavaScript / Node.js
JavaScript's Date object works in milliseconds. Divide by 1000 for Unix seconds. Use Intl.DateTimeFormat or date-fns for formatting.
Unix Timestamp Converter in Python
Python's datetime module and time module both handle Unix timestamps. Use datetime.fromtimestamp() for local time and datetime.utcfromtimestamp() for UTC.
Unix Timestamp Converter in Go
Go's time package uses int64 for Unix timestamps. time.Unix() converts to a time.Time value; .Unix() on a time.Time returns the epoch seconds.
Unix Timestamp Converter in PHP
PHP's time() returns the current Unix timestamp. Use date() or DateTime for formatting and strtotime() for parsing.
Unix Timestamp Converter in Ruby
Ruby's Time class works natively with Unix timestamps. Use Time.now.to_i for the current epoch and Time.at to convert back.
Unix Timestamp Converter in Rust
Rust's std::time::SystemTime provides the raw timestamp. The chrono crate adds human-readable formatting and timezone support.
Unix Timestamp Converter in C# / .NET
C# uses DateTimeOffset for timezone-aware timestamps. ToUnixTimeSeconds() and FromUnixTimeSeconds() handle Unix epoch conversion.