Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get the number of seconds passed since 1970 for a date value?

Tags:

android

I have to an android application in which I need to convert the current date to the number of seconds passed since 1970.

What I am doing currently is this.

Calendar cal = new GregorianCalendar(TimeZone.getTimeZone("GMT")); cal.set(currentDate.get(Calendar.YEAR), month, currentDate.get(Calendar.DAY_OF_MONTH), 0, 0, 0); long timesince1970 = cal.getTime().getTime(); 

Another problem is that my application needs to run in Germany. Hence the need to convert the time to their time zone and then convert it to the number of seconds since 1970.

The above is not giving me correct results.

like image 260
user590849 Avatar asked Jul 22 '11 05:07

user590849


People also ask

How do you calculate seconds from dates?

Multiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.

How do you find the number of days from 1970?

Days Since 1970-01-01 There were 19230 days since January 1, 1970, the Unix epoch.

How many days has it been since the epoch?

For example, today, Fri 23rd April 2021 - timestamp at 00:00:00 UTC was 1619136000. As expected, this is a multiple of 86400, and 1619136000 / 86400 = 18740. There have been 18740 days since the unix epoch.


2 Answers

System.currentTimeMillis() gives you the number of milliseconds since the Unix epoch (January 1, 1970 00:00:00 UTC).

Divide it by 1000 to get the number of seconds.

like image 76
Jean Hominal Avatar answered Sep 30 '22 17:09

Jean Hominal


//long currentTimeMillis ()-Returns the current time in milliseconds.  long millis = System.currentTimeMillis();  //Divide millis by 1000 to get the number of seconds. long seconds = millis / 1000; 
like image 25
Faxriddin Abdullayev Avatar answered Sep 30 '22 16:09

Faxriddin Abdullayev