Search code examples
javaandroidtimesystemunix-timestamp

Why is Android System.currentTimeMillis() not an accurate Timestamp?


I've encountered what appears to be a very unusual bug while developing an android application.

The following code in my MainActivity onCreate method:

long ts = System.currentTimeMillis();
System.out.println(ts);

The returned value is 1570469720537.

This time would be in the year 51736 according to unixtimestamp.com

Has anybody encountered this problem? Any ideas what may be causing it?


Solution

  • While epoch time is measured in seconds, System.currentTimeMillis() returns the time in milliseconds for more accuracy, which is the value you see in the example. If you divide it by 1000 you will get the time in seconds, which will convert to the current epoch time you expect.

    You can paste the value in here to see the timestamp: https://www.epochconverter.com/