Unix Timestamp in Java
Use `java.time` (JSR-310, since Java 8). Avoid the legacy `java.util.Date` - it's mutable and timezone-confused.
Common Operations
Get current Unix timestamp (seconds)
long ts = Instant.now().getEpochSecond(); // 1706745600
Get current Unix timestamp (milliseconds)
long tsMs = Instant.now().toEpochMilli(); // 1706745600000
Convert Unix timestamp to Instant
Instant instant = Instant.ofEpochSecond(1706745600); // 2024-01-31T22:40:00Z
Convert Instant to Unix timestamp
long ts = Instant.parse("2024-01-31T22:40:00Z").getEpochSecond();
// 1706740800
Format as ISO 8601
String iso = Instant.now().toString(); // '2024-01-31T22:40:00.123Z'
Convert to specific timezone
ZonedDateTime zdt = Instant.ofEpochSecond(1706745600)
.atZone(ZoneId.of("America/Los_Angeles"));
// 2024-01-31T14:40:00-08:00[America/Los_Angeles]