Java Program to Convert Milliseconds to Minutes and Seconds

To understand this example, you should have the knowledge of the following Java programming topics:


In Java, we can use the built-in methods:

  • toMinutes() - to convert milliseconds to minutes
  • toSeconds() - to convert milliseconds to seconds

Example 1: Milliseconds to Seconds/Minutes Using toMinutes() and toSeconds()

import java.util.concurrent.TimeUnit;

class Main {
  public static void main(String[] args) {

    long milliseconds = 1000000;

    // us of toSeconds()
    // to convert milliseconds to minutes
    long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);
    System.out.println(milliseconds + " Milliseconds = " + seconds + " Seconds");

    // use of toMinutes()
    // to convert milliseconds to minutes
    long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
    System.out.println(milliseconds + " Milliseconds = " + minutes + " Minutes");
  }
}

Output

1000000 Milliseconds = 1000 Seconds
Or
1000000 Milliseconds = 16 Minutes

In the above program, we have used the long datatype to store milliseconds, minutes, and seconds values. It is because the toMinutes() and toSeconds() methods return values in long.

Note: To use the methods, we must import the java.util.concurrent.TimeUnit package.


We can also use the basic mathematical formulas to convert milliseconds to minutes and seconds.

// convert milliseconds to seconds
Seconds = milliseconds / 1000

// convert seconds to minutes
minutes = seconds / 60

// convert millisecons to minutes
minutes = (milliseconds / 1000) / 60

Example 2: Milliseconds to Seconds/Minutes Using Mathematical Formula

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

    long seconds = (milliseconds / 1000);
    System.out.println(milliseconds + " Milliseconds = " + seconds + " Seconds");

    long minutes = (milliseconds / 1000) / 60;
    System.out.println(milliseconds + " Milliseconds = " + minutes + " Minutes");
  }
}

Output

1000000 Milliseconds = 1000 Seconds
1000000 Milliseconds = 16 Minutes

In the above program, we have

  • converted milliseconds to seconds by dividing it by 1000
  • converted to minutes by dividing the seconds by 60

Also Read:

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges