0

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

Thang
  • 409
  • 1
  • 6
  • 17
  • Does this answer your question? [How to convert milliseconds to “hh:mm:ss” format?](https://stackoverflow.com/questions/9027317/how-to-convert-milliseconds-to-hhmmss-format) – Ole V.V. Apr 07 '21 at 13:59
  • Use the TimeUnit class " long minutes = TimeUnit.MILLISECONDS.toMinutes(millis); long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);" – Narendra_Nath Apr 07 '21 at 14:06
  • 1
    @Narendra_Nath The `TimeUnit` enum is an option, but not the best one. At least since Java 9 the `Duration` class is better suited for this task. – Ole V.V. Apr 08 '21 at 01:19
  • @OleV.V. Thank you. I'll definitely look into it – Narendra_Nath Apr 08 '21 at 05:22

3 Answers3

2

Using the Duration type from kotlin.time package, you can first convert the number of milliseconds to a Duration and then use its toComponents method to split it into minutes and seconds:

import kotlin.time.*

fun main() {
    val millis = 260100
    val duration = millis.toDuration(DurationUnit.MILLISECONDS)
    val timeString = 
        duration.toComponents { minutes, seconds, _ -> 
            String.format("%02d:%02d", minutes, seconds)
        }
    println(timeString)  // prints 04:20
}
Ilya
  • 21,871
  • 8
  • 73
  • 92
0

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.

val minutes = i / 1000 / 60
val seconds = i / 1000 % 60
val formatted = String.format("%02d:%02d", minutes, seconds)

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:

private fun startCounting() = lifecycleScope.launch {
    val startTime = System.currentTimeMillis()
    var remainingTime = 60100L 
    while (remainingTime >= 0) {
        val minutes = remainingTime / 1000 / 60
        val seconds = remainingTime / 1000 % 60
        myTextView.text = String.format("%02d:%02d", minutes, seconds)
        delay(1000L)
        remainingTime = (60100L - System.currentTimeMillis() + startTime)
    }
}
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
0

Please get a simple and scalable solution for Kotlin.

Helper function for getting duration from mills. If your duration is in Int then follow the same or change Int to Long.

fun Int.getDuration(): String {
    val date = Date(this.toLong())
    val formatter = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
    formatter.setTimeZone(TimeZone.getTimeZone("UTC"))
    return formatter.format(date)
}

How to use this function

Log.d("YOUR_TAG", "${millis.getDuration()}, ${currentMillis.getDuration()}")

OUTPUT -> 00:05:01, 00:00:25

Note* - Do not forget to add the TimeZone. If you have any query then you can comment below.

Pushpendra
  • 1,492
  • 1
  • 17
  • 33