j2emanue
j2emanue

Reputation: 62559

Android - exchanging System. currentTimeMillis() for SystemClock.uptimeMillis() - what are the implications

i have tried to write the following function which given a time in the future will tell me how many milliseconds until that time:

fun millisFromNow(futureTime: Long): Long {
    val now = Date(System.currentTimeMillis())
    val endDate = Date(futureTime * 1000L)
    return (endDate.time - now.time)
}

but the user can change the system clock and then currentTimeMillis will not be accurate ..... instead could i use uptimeMillis :

fun millisFromNow(futureTime: Long): Long {
        val now = Date(SystemClock.uptimeMillis())
        val endDate = Date(futureTime * 1000L)
        return (endDate.time - now.time)
    }

it seems it will work on almost all devices since 2004 on android right ? am i using it correctly in this fashion ? because currentTimeMillis is epoch time but uptimeMillis is time since last boot and assumes JVM is running. so to me this code does not make sense. ? im just using this function to start a countdownTimer so i just call it once.

update: just replacing it gives the wrong value. just system.currentTimeMillis is giving right countdown. what can i replace it with for security sake so i can get the right millisec countdown between now and a time in the future

Upvotes: 0

Views: 652

Answers (0)

Related Questions