Convert Milliseconds to Minutes and Seconds Using Kotlin

In this article, we will learn how to convert milliseconds to minutes and seconds separately, and simultaneously in Kotlin.

Example 1

In the below program, we have converted given milliseconds to minutes using toMinutes() and seconds using toSeconds() method.

import java.util.concurrent.TimeUnit

fun main(args: Array<String>) {

    val milliseconds: Long = 1000000

    // long minutes = (milliseconds / 1000) / 60;
    val minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds)

    // long seconds = (milliseconds / 1000);
    val seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds)

    println("$milliseconds Milliseconds = $minutes minutes")
    println("AND")
    println("$milliseconds Milliseconds = $seconds seconds")
}

Output

When you run the program, the output will be

1000000 Milliseconds = 16 minutes
AND
1000000 Milliseconds = 1000 seconds

Example 2

fun main(args: Array<String>) {

    val milliseconds: Long = 1000000

    val minutes = milliseconds / 1000 / 60
    val seconds = milliseconds / 1000 % 60

    println("$milliseconds Milliseconds = $minutes minutes and $seconds seconds")
}

Output

When you run the program, the output will be

1000000 Milliseconds = 16 minutes and 40 seconds

The reference of Java code is stack overflow.

In the above example, we have to calculate the minutes first by simply dividing them into seconds. After that divide minutes by 60. Now we have to calculate the remaining seconds by dividing them into seconds and we can get the remainder when we divide it by 60.

That’s it. This is how to Converts Milliseconds to Minutes and Seconds in Kotlin Program

If you have any questions or suggestions, feel free to ask in the comments section below.

Next Article

Convert Milliseconds into Minutes and Seconds in Java

Please share this post:
Posts created 41

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: