Convert Milliseconds to Minutes and Seconds Using Java

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

Example 1

In the below code, we have converted milliseconds to minutes using the method toMinutes(). To convert it into seconds we use the methodtoSeconds() .

import java.util.concurrent.TimeUnit;

public class MillisecondsInJava {

    public static void main(String[] args) {
        long milliseconds = 1000000;

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

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

        System.out.format("%d Milliseconds = %d minutes\n", milliseconds, minutes );
        System.out.println("AND");
        System.out.format("%d Milliseconds = %d seconds", milliseconds, seconds );

    }
}

Output

1000000 Milliseconds = 16 minutes
AND
1000000 Milliseconds = 1000 seconds

Example 2

In this 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.

public class MillisecondsInJava {

    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 reference of Java code is stack overflow.

Output

1000000 Milliseconds = 16 minutes and 40 seconds.

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

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

Next Article

Convert the Milliseconds in Minutes and Seconds in Kotlin

Please share this post:
Posts created 31

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: