Convert Milliseconds to Minutes and Seconds in Java
-
Use the
java.util.concurrent
Package to Convert Milliseconds to Minutes and Seconds - Use a Mathematical Formula to Convert Milliseconds to Minutes and Seconds
-
Use the
java.time
Library Methods to Convert Milliseconds to Minutes and Seconds

This article introduces the three different methods to convert Milliseconds to minutes and seconds in Java, and we will look at them one by one with the example code.
Use the java.util.concurrent
Package to Convert Milliseconds to Minutes and Seconds
The concurrent
package of the java.util
library contains the TimeUnit
class to manage times in Java. Users can use the toSeconds()
and toMinutes()
methods to convert the milliseconds to seconds and minutes, respectively; both toSeconds()
and toMinutes()
methods take the milliseconds as a parameter.
Users should follow the syntax below to convert Milliseconds to minutes and seconds using the TimeUnit
class.
Syntax of toSeconds()
and toMinutes()
:
long seconds = TimeUnit.MILLISECONDS.toSeconds(mlSeconds);
long minutes = TimeUnit.MILLISECONDS.toMinutes(mlSeconds);
Parameter:
mlSeconds |
It is a millisecond to convert Minutes and seconds. |
Example Code:
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) {
long mlSeconds = 343232232;
// Converting Milliseconds to seconds
int seconds = (int)TimeUnit.MILLISECONDS.toSeconds(mlSeconds);
System.out.format("%d Milliseconds = %d seconds\n", mlSeconds, seconds );
// converting Milliseconds to minutes
int minutes = (int)TimeUnit.MILLISECONDS.toMinutes(mlSeconds);
System.out.format("%d Milliseconds = %d minutes\n", mlSeconds, minutes );
System.out.format("%d Milliseconds = %d minutes and %d seconds", mlSeconds, minutes, seconds%60 );
}
}
Output:
343232232 Milliseconds = 343232 seconds
343232232 Milliseconds = 5720 minutes
343232232 Milliseconds = 5720 minutes and 32 seconds
In the above output, users can observe that we have converted the milliseconds to only seconds, minutes, and minutes and seconds.
Use a Mathematical Formula to Convert Milliseconds to Minutes and Seconds
The best way to convert milliseconds to minutes and seconds is by using mathematical formulas rather than internal or external libraries.
In the example below, we have divided the milliseconds by 1000*60
to get the minutes, as a single minute contains 60000 milliseconds. After that, to get the remaining seconds, we have divided milliseconds by 1000 and taken its modulo by 60, which will return the remaining seconds.
Example Code:
public class Test {
public static void main(String[] args)
{
long mlSeconds = 2323132;
// Divide milliseconds by 1000 and perform modulo by 60 to get seconds
long seconds = (mlSeconds / 1000) % 60;
// Divide milliseconds by 1000*60 to convert to minutes as 1 minute contains the 60000 milliseconds
long minutes = mlSeconds / (1000*60);
System.out.format("%d Milliseconds = %d minutes and %d seconds", mlSeconds, minutes,seconds );
}
}
In the output, users see 2323132 milliseconds equals 38 minutes and 43 seconds.
Output
2323132 Milliseconds = 38 minutes and 43 seconds
Use the java.time
Library Methods to Convert Milliseconds to Minutes and Seconds
The java.time
library contains the Duration
class, which we can use to get the duration between two times. Also, it contains a built-in method to convert the time into seconds or minutes.
In the example below, we have taken the duration between two times and stored it inside the dura
object. To get the total seconds, we have invoked the getSeconds()
method on the dura
object, and in the same way, to get the total minutes of duration, we have used the toMinutes()
.
Example Code:
import java.time.*;
public class DurationExample4 {
public static void main(String[] args) {
// get duration between two times
Duration dura = Duration.between(LocalTime.MIN,LocalTime.NOON);
// get the total seconds of duration
System.out.println("Total duration in seconds is: " + dura.getSeconds());
// get the total minutes for the duration
System.out.println("Total duration in minutes is: " + dura.toMinutes());
}
}
Output:
Total duration in seconds is: 43200
Total duration in minutes is: 720
Users have learned the three different methods to convert milliseconds into either minutes, seconds, or minutes and seconds. It is recommended to use the mathematical formula as it can give the best performance among all three methods.