Unix Timestamp in Scala
Scala runs on the JVM and shares Java's time libraries. Use java.time.Instant (preferred since Java 8) or System.currentTimeMillis() for Unix timestamps. The Scala standard library has no dedicated time API.
Code Examples
Current timestamp (seconds)
import java.time.Instant Instant.now().getEpochSecond
java.time.Instant is the modern, idiomatic approach. getEpochSecond returns a Long representing seconds since the Unix epoch.
Current timestamp (milliseconds)
System.currentTimeMillis()
Returns milliseconds since the Unix epoch as a Long. Equivalent to Instant.now().toEpochMilli — slightly faster as it bypasses the Instant object.
Convert timestamp to Instant
import java.time.Instant val instant = Instant.ofEpochSecond(1708560000L)
Reconstruct an Instant from a Unix timestamp in seconds. Use ofEpochMilli for millisecond-precision inputs.
Format timestamp as date string
import java.time.{Instant, ZoneOffset}
import java.time.format.DateTimeFormatter
val fmt = DateTimeFormatter.ISO_LOCAL_DATE_TIME.withZone(ZoneOffset.UTC)
fmt.format(Instant.now())Format the current instant as an ISO 8601 string in UTC. Replace ZoneOffset.UTC with ZoneId.of("America/New_York") for local time.
Using scala.concurrent.duration (relative)
import scala.concurrent.duration._ val d = 5.minutes println(d.toSeconds) // 300
FiniteDuration is useful for relative time calculations (timeouts, delays) but does not represent Unix timestamps. Combine with System.currentTimeMillis() for deadline logic.
Note
Scala's JVM heritage means java.time.Instant is the idiomatic, thread-safe choice for Unix timestamps. Avoid the legacy java.util.Date and java.util.Calendar APIs — they're mutable and have timezone quirks. If you're on Scala.js, use js.Date.now() instead.
Need to convert a specific timestamp? Use the live converter — paste any epoch value and see the human-readable date instantly.
← Open the converter