For Developers

Unix Timestamp in Scala

How to get the current timestamp, convert it to a date, and convert a date back to a timestamp in Scala — using java.time under the hood, the way idiomatic Scala code does.

import java.time.Instant
val ts: Long = Instant.now().getEpochSecond
import java.time.{{Instant, ZoneOffset}}
val seconds = 1750000000L
val dt = Instant.ofEpochSecond(seconds).atZone(ZoneOffset.UTC)
import java.time.{{ZonedDateTime, ZoneOffset}}
val zdt = ZonedDateTime.of(2025, 6, 15, 12, 0, 0, 0, ZoneOffset.UTC)
val ts = zdt.toEpochSecond

The Scala-specific pitfall

Scala has no date/time types of its own — it uses Java's, and that means the same java.util.Date vs. java.time.Instant split applies here too. The extra Scala-specific wrinkle shows up in Spark and distributed pipelines: a Long epoch column read from one data source in seconds and another in milliseconds will join and aggregate without any type error, just silently wrong timestamps three orders of magnitude off. When a schema doesn't document the unit explicitly, check a sample value's digit count (10 digits ≈ seconds, 13 ≈ milliseconds) before trusting it.

Have a timestamp right now?

Paste it into the Timestamp ⇄ Date Converter to check it instantly, in any timezone, without writing any code. Working across several languages on the same project? The full Epoch Time in Programming Languages guide has all of them side by side for quick comparison.

Related

Related tools & reading