Android get time in milliseconds

Spread the love

If you need to get the time in milliseconds in android then you can use the following code snippet:

long nowTimeStamp = SystemClock.elapsedRealtime();

Now above code snipped will give you time since the system has been rebooted in milliseconds. In some case it would be sufficient to find how may milliseconds has been taken by any operation.

Now there are other APIs too to get the time in milliseconds e.g.

SystemClock.uptimeMillis();
System.currentTimeMillis();

Now difference between SystemClock.uptimeMillis() and SystemClock.elapsedRealtime() is:

SystemClock.uptimeMillis() is counted in milliseconds since the system was booted. This looks quite similar with SystemClock.elapsedRealtime() but this clock stops when the system enters deep sleep.

System  |  Android Developers, SystemClock  |  Android Developers

So, whenever you need to use interval in milliseconds, then use ‘SystemClock.elapsedRealtime’.

The issue with ‘System.currentTimeMillis’ is that it depends on system time set by user. If user changes this time, then your interval time will be not be correct.

So, choose carefully according to your needs.

Cheers and Peace out!!!