Skip to content
DeveloperMemos

How to Format Milliseconds into Minutes and Seconds in Kotlin

Kotlin, Time Formatting2 min read

When working with time data, it's often necessary to format values such as milliseconds into a more human-readable format such as minutes and seconds. In Kotlin, there are several ways to accomplish this task, depending on your specific use case. In this article, we'll explore some of the common approaches to formatting milliseconds into minutes and seconds in Kotlin.

First, let's take a look at the basic formula for converting milliseconds into minutes and seconds. To convert milliseconds to minutes and seconds, we need to divide the total number of milliseconds by 1000 to get the total number of seconds, and then divide the total number of seconds by 60 to get the total number of minutes. We can then use the modulo operator (%) to get the remaining seconds after calculating the minutes.

For example, let's say we have a value of 125,000 milliseconds. To convert this value into minutes and seconds, we would first divide 125,000 by 1000 to get 125 seconds. We can then divide 125 by 60 to get 2 minutes, with 5 seconds remaining. Therefore, 125,000 milliseconds is equal to 2 minutes and 5 seconds.

Now that we understand the basic formula, let's explore some ways to implement this in Kotlin.


  1. Using basic arithmetic:

One way to format milliseconds into minutes and seconds is to use basic arithmetic operations. We can create a function that takes a value of milliseconds as input and returns a formatted string of minutes and seconds. Here's an example:

1fun formatMilliseconds(milliseconds: Long): String {
2 val totalSeconds = milliseconds / 1000
3 val minutes = totalSeconds / 60
4 val seconds = totalSeconds % 60
5 return "$minutes:$seconds"
6}

In this example, we first calculate the total number of seconds by dividing the value of milliseconds by 1000. We then calculate the total number of minutes by dividing the total number of seconds by 60, and the remaining seconds by using the modulo operator. Finally, we return a formatted string that combines the minutes and seconds values with a colon separator.


  1. Using the Duration class:

Another way to format milliseconds into minutes and seconds is to use the Duration class provided by the Kotlin standard library. The Duration class represents a duration of time with a specific number of seconds and nanoseconds. We can create a Duration object by passing the value of milliseconds as a parameter to the constructor, and then use the toMinutes and toSeconds methods to get the values of minutes and seconds. Here's an example:

1fun formatMilliseconds(milliseconds: Long): String {
2 val duration = Duration.ofMillis(milliseconds)
3 val minutes = duration.toMinutes()
4 val seconds = duration.minusMinutes(minutes).seconds
5 return "$minutes:$seconds"
6}

In this example, we first create a Duration object by passing the value of milliseconds to the constructor. We can then use the toMinutes method to get the total number of minutes, and the minusMinutes method to subtract the minutes from the duration and get the remaining seconds. Finally, we return a formatted string that combines the minutes and seconds values with a colon separator.


  1. Using the DateFormat class:

If you need more advanced formatting options, you can also use the DateFormat class provided by the Java standard library. The DateFormat class allows you to format dates and times in a variety of formats, including custom formats. We can create a SimpleDateFormat object with a custom format string that specifies the minutes and seconds components, and then use the format method to format the value of milliseconds. Here's an example:

1fun formatMilliseconds(milliseconds: Long): String {
2 val format = SimpleDateFormat("mm:ss")
3 return format.format(Date(milliseconds))
4}

In this example, we first create a SimpleDateFormat object with a custom format string that specifies the minutes and seconds components. We can then pass the value of milliseconds to the Date constructor to create a Date object, and use the format method to format the date in the desired format. Finally, we return the formatted string.