I have the following code but I don't understand how it went wrong with the result I wanted, At first I have a total time which is supposed to be of type Long (Milisecond) and then I need to convert it to a format mm: ss is not a format (HH: mm: ss), So who can help I convert it to mm:ss format
private fun startCounting() {
var i = 60100
val totalSeconds =
TimeUnit.MINUTES.toSeconds(i.toLong())
Handler().postDelayed({
val tickSeconds = 0
for (second in tickSeconds until totalSeconds) {
val time =
String.format(
"%02d:%02d", TimeUnit.MILLISECONDS.toMinutes(second),
TimeUnit.MILLISECONDS.toSeconds(second) % TimeUnit.MINUTES.toMinutes(1)
)
}
}, 1000)
}
Thanks You
The number of minutes is the number of milliseconds divided by 60000. The number of seconds is the number of milliseconds divided by 1000. But to remove minutes from the number of seconds, you want the remainder of dividing that by 60.
It also looks like maybe you're trying to display a countdown with updated value once per second. But looping through the numbers in a for-loop synchronously will just instantly show the final value. Instead, you can use a coroutine with delays to do it: