Convert Milliseconds to Minutes and Seconds individually in Java

thumbnail

In this tutorial, you will read about converting Milliseconds to Minutes and Seconds in Java. We will convert the given value milliseconds to minutes using toMinutes() method. Then, to convert the given value milliseconds to seconds, we can use toSeconds() methods.

Example 1: Convert milliseconds to minutes and seconds individually in Java

import java.util.concurrent.TimeUnit;
public class Milliseconds {
    public static void main(String[] args) {
        long milliseconds = 1000000;
        
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
        
        long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);
        System.out.format("%d Milliseconds = %d minutes\n", milliseconds, minutes );
        System.out.format("%d Milliseconds = %d seconds", milliseconds, seconds );
    }
}

The output will be:

1000000 Milliseconds = 16 minutes
1000000 Milliseconds = 1000 seconds

We can also use simple math to convert milliseconds to minutes and seconds individually.

Seconds = Milliseconds / 1000
Minutes = Seconds / 60
or
Minutes = (Milliseconds / 1000) / 60

Example 2: Convert milliseconds to minutes and seconds in Java

public class Milliseconds {
    public static void main(String[] args) {
        long milliseconds = 1000000;
        long minutes = (milliseconds / 1000) / 60;
        long seconds = (milliseconds / 1000) % 60;
        System.out.format("%d Milliseconds = %d minutes and %d seconds.", milliseconds, minutes, seconds);
    }
}

The output will be:

1000000 Milliseconds = 16 minutes and 40 seconds.

Read More

Back To Top