Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala UTC timestamp in seconds since January 1st, 1970

How do I get the Scala UTC timestamp in seconds since January 1st, 1970?

like image 282
user1491739 Avatar asked Jul 05 '12 20:07

user1491739


People also ask

How do I calculate UTC timestamp?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

What is timestamp UTC?

By convention, meteorologists use just one time zone: Universal Time, Coordinated (UTC). They also use the twenty four hour clock (where 0000 = midnight UTC). The date/time stamp on each forecast image represents the time at which the forecast is valid, measured in UTC.

Is timestamp in seconds or milliseconds?

The UNIX timestamp is an integer that represents the number of seconds elapsed since January 1 1970. The timestamp in JavaScript is expressed in milliseconds.

Are epoch timestamps always UTC?

Technically, no. Even though epoch time is the means elapsed seconds since 1/1/70 00:00:00 the real "GMT" (UTC) is not. UTC time needed to be changed a few times to take in to account the slowing speed of the rotating earth. As everybody wrote, most people use epoch at UTC.


2 Answers

Same way as you would in Java:

val timestamp: Long = System.currentTimeMillis / 1000 
like image 100
Dan Simon Avatar answered Oct 10 '22 23:10

Dan Simon


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

like image 36
Ceasar Bautista Avatar answered Oct 11 '22 00:10

Ceasar Bautista