java.time (Java 8+). Working examples for every common operation — get current time, convert epoch to date, convert date to epoch, format display, parse, and handle timezones.
Use `Instant.now()`. It represents a point on the timeline, independent of timezone.
import java.time.Instant;
long secs = Instant.now().getEpochSecond();
// → 1735689600
long ms = Instant.now().toEpochMilli();
// → 1735689600000
// or the older:
System.currentTimeMillis();
Instant now = Instant.now();
long micros = now.getEpochSecond() * 1_000_000L + now.getNano() / 1000;
long nanos = now.getEpochSecond() * 1_000_000_000L + now.getNano();
Build an Instant from epoch, then convert to a ZonedDateTime when you need a timezone.
Instant inst = Instant.ofEpochSecond(1735689600);
System.out.println(inst);
// → 2025-01-01T00:00:00Z
Instant inst = Instant.ofEpochMilli(1735689600000L);
import java.time.ZonedDateTime;
import java.time.ZoneId;
ZonedDateTime zdt = Instant.ofEpochSecond(1735689600)
.atZone(ZoneId.of("America/New_York"));
System.out.println(zdt);
// → 2024-12-31T19:00-05:00[America/New_York]
Any Instant has `.getEpochSecond()` and `.toEpochMilli()`.
ZonedDateTime zdt = ZonedDateTime.now();
long secs = zdt.toEpochSecond();
long ms = zdt.toInstant().toEpochMilli();
import java.time.LocalDateTime;
import java.time.ZoneOffset;
long secs = LocalDateTime.of(2025, 1, 1, 0, 0, 0)
.toEpochSecond(ZoneOffset.UTC);
// → 1735689600
Use `DateTimeFormatter` — Java's built-in formatter.
import java.time.format.DateTimeFormatter;
String iso = ZonedDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
// → "2025-01-01T12:34:56.789-05:00"
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss z");
String s = ZonedDateTime.now().format(fmt);
`Instant.parse()` handles ISO 8601 directly. For other formats, use `DateTimeFormatter`.
Instant inst = Instant.parse("2025-01-01T00:00:00Z");
long secs = inst.getEpochSecond();
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime ldt = LocalDateTime.parse("2025-01-01 12:00:00", fmt);
long secs = ldt.toEpochSecond(ZoneOffset.UTC);
Java date pitfalls.
// Old Date is mutable, timezone-broken, deprecated for new code.
// Use Instant, LocalDateTime, ZonedDateTime instead.
// LocalDateTime.now() returns the local clock reading
// but has no tzinfo — you can't convert it to epoch
// without specifying a ZoneId.
LocalDateTime ldt = LocalDateTime.now();
// ldt.toEpochSecond(ZoneOffset.UTC) — explicit zone required
// Don't worry about your machine's timezone for Instant.
// It always represents a UTC point on the timeline.
Need to quickly check what a specific timestamp converts to? Use the main converter — paste any value and get every format back.
Working with many timestamps at once? Try the batch converter — paste a list, get a CSV or JSON file back.