1. Overview
Converting milliseconds to minutes and seconds can be very useful while dealing with time-related calculations in a Kotlin application. Furthermore, it can help improve the readability of time-related data by making them more user-friendly to interpret.
In this tutorial, we’ll learn multiple ways to convert milliseconds to minutes and seconds.
2. Using Arithmetic Operations
First, let’s initialize the millis variable with a sample value denoting milliseconds:
val millis:Long = 123456L
Now, we can apply the division operator to convert milliseconds to minutes:
val mins = millis / 60000
It’s important to note that Kotlin truncates the decimal portion when we divide between Int and Long values. So, mins will contain the whole number of minutes.
Next, we can calculate the remaining seconds value from millis using modulus (%) and division (/) operators:
val secs = (millis % 60000) / 1000
Finally, let’s validate that we’ve got the correct values in mins and secs variables:
assertEquals(2, mins)
assertEquals(3, secs)
The output looks correct.
3. Using kotlin.time From the Standard Library
In this section, we’ll use the kotlin.time package from the Kotlin standard library for solving our use case of converting milliseconds to minutes and seconds.
3.1. Using Duration and DurationUnit Classes
Let’s start by creating a duration object from millis using the toDuration() function:
val duration = millis.toDuration(DurationUnit.MILLISECONDS)
We must note that we passed DurationUnit.MILLISECONDS to specify that we’re measuring millis in milliseconds.
Further, we can get the minutes using the inWholeMinutes property of the duration object:
val mins = duration.inWholeMinutes
Furthermore, we can calculate the remaining duration using the minus() method and get the remaining seconds from the inWholeSeconds property:
val secs = duration.minus(mins.toDuration(DurationUnit.MINUTES)).inWholeSeconds
Lastly, let’s verify that our approach is giving the right results:
assertEquals(2, mins)
assertEquals(3, secs)
Fantastic! It looks correct.
3.2. Using Extension Properties from Duration.Companion
Alternatively, we can use the extension properties from Duration.Companion to create duration objects conveniently.
First, to get the minutes, we can create a Duration object using millis.milliseconds and then retrieve the inWholeMinutes property:
val mins = millis.milliseconds.inWholeMinutes
Next, we can get the remaining seconds by subtracting the inWholeSeconds property value of the mins.minutes duration from that of millis.milliseconds:
val secs = millis.milliseconds.inWholeSeconds - mins.minutes.inWholeSeconds
Finally, let’s verify the result:
assertEquals(2, mins)
assertEquals(3, secs)
Perfect! It works as expected.
4. Using kotlinx-datetime Library
The kotlinx-datetime library is part of the Kotlin ecosystem and provides convenient APIs for working with date and time operations. In this section, we’ll use it to retrieve the minutes and seconds from the epoch milliseconds.
4.1. Maven Dependency
To use the kotlinx-datetime library, we must add the dependency for kotlinx-datetime-jvm in the project’s pom.xml:
<dependency>
<groupId>org.jetbrains.kotlinx</groupId>
<artifactId>kotlinx-datetime-jvm</artifactId>
<version>0.4.0</version>
</dependency>
It’s now available for use.
4.2. Minutes and Seconds From Epoch Milliseconds
First, let’s set epochMillis with the epoch value:
val epochMillis: Long = 123456L
It’s important to note that this epoch value represents the “Thu Jan 01 1970 00:02:03” timestamp in UTC.
Now, let’s create an instant object using the fromEpochMilliseconds() method of the Instant class:
val instant = Instant.fromEpochMilliseconds(epochMillis)
The instant object represents an absolute timestamp in UTC (Coordinated Universal Time).
Further, we can obtain an equivalent localDateTime object using the TimeZone.UTC timezone and our instant object:
val localDateTime = instant.toLocalDateTime(TimeZone.UTC)
Next, we can use the hour, minute, and second properties of the localDateTime object to calculate the minutes and seconds:
val mins = localDateTime.hour * 60 + localDateTime.minute.toLong()
val secs = localDateTime.second.toLong()
We must note that the resultant values represent minutes and seconds elapsed on a specific day by the UTC clock.
Finally, let’s validate the results:
assertEquals(2, mins)
assertEquals(3, secs)
Great! We got this one right.
5. Conclusion
In this article, we started with an approach of using arithmetic operations to convert the minutes and seconds from milliseconds. Furthermore, we explored time-based APIs from the kotlin.time and kotlinx.datetime packages available in Kotlin standard and kotlinx-datetime libraries, respectively.
As always, the code from this article is available over on GitHub.