How do I get the Scala UTC timestamp in seconds since January 1st, 1970?
Asked
Active
Viewed 8.2k times
2 Answers
101
Same way as you would in Java:
val timestamp: Long = System.currentTimeMillis / 1000

Dan Simon
- 12,891
- 3
- 49
- 55
-
Are you sure that this is the UTC timestamp, and not the timestamp of the time set on the server? – user1491739 Jul 05 '12 at 20:41
-
1The value depends on the system clock but the value is calculated since 1/1/70 UTC. From the JavaDoc "Returns: the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. " (Note: I'm not at all sure that this accounts for leap seconds such as the one just added the other night. So I'm not at all sure that I would trust it for, e.g., astronomical calculations.) – Larry OBrien Jul 05 '12 at 20:53
-
1@LarryOBrien it seems it depends on the underlying OS to take leap-seconds into account, but from the `Date` [javadoc](http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Date.html) "Most computer clocks are not accurate enough to be able to reflect the leap-second distinction." – Dan Simon Jul 05 '12 at 21:12
-
Don't think leap seconds matter in Unix timestamps. The millis timestamp keeps going forward 1000 per second regardless of what humans say or do with their AM or PM, etc. – Andrey Fedorov Mar 10 '18 at 21:54
-
oops looks like I'm wrong — https://stackoverflow.com/questions/16539436/ – Andrey Fedorov Mar 10 '18 at 21:55
41
As of Java 8 it's possible to do so like so:
import java.time.Instant
unixTimestamp : Long = Instant.now.getEpochSecond
Via micha's answer here: https://stackoverflow.com/a/24703573/577199
-
I think this may be outdated. I'm not seeing the option for this method on that class. – bgenchel Apr 14 '23 at 21:55