11

How can I get the absolute system boot time within the shell? i tried cat/proc/uptime, but this only returns the time since reboot.

Similitran
  • 165
  • 1
  • 1
  • 9

1 Answers1

20

It's basically:

java.lang.System.currentTimeMillis() - android.os.SystemClock.elapsedRealtime();
flx
  • 14,146
  • 11
  • 55
  • 70
  • @fix : Both give time in different frmats. – Ankit Bansal Dec 24 '13 at 08:27
  • 7
    But I think it will not give proper result if user has changed a time. – keen May 28 '14 at 06:36
  • take into account that the user could change the time and currentTimeMillis() wont be correct, you should fix that by listening to ACTION_TIME_CHANGED http://developer.android.com/reference/android/content/Intent.html#ACTION_TIME_CHANGED – Chiara Dec 18 '14 at 12:37
  • 1
    System.currentTimeMillis() is not affected by timezone, it returns UTC timestamp. – Kurovsky Feb 10 '17 at 08:35
  • Except that there is a non-zero amount of time between the two calls. – Michael Oct 09 '17 at 05:02
  • That's right. "elapsedRealtimeNanos return the time since the system was booted, and include deep sleep. This clock is guaranteed to be monotonic, and continues to tick even when the CPU is in power saving modes, so is the recommend basis for general purpose interval timing." – Zhou Hongbo Sep 23 '19 at 07:48
  • Case1: user changes time from settings according to @keen; Case2: the `System.currentTimeMills()` is not current real time because the stupid rom, I have a device that the system time begin from a specific value every time reboot the system. – DysaniazzZ Oct 28 '19 at 02:57
  • java.lang.System.currentTimeMillis() changes when changing the system clock from settings. so the result could be incorrect in this case. – Mahmoud Jan 31 '20 at 07:50