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.
Multiply the two dates' absolute difference by 86400 to get the Epoch Time in seconds – using the example dates above, is 319080600.
Days Since 1970-01-01 There were 19230 days since January 1, 1970, the Unix 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.
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.
//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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With