Saturday, November 16, 2024
Google search engine
HomeLanguagesJavaJava Program to Convert Milliseconds to Minutes and Seconds

Java Program to Convert Milliseconds to Minutes and Seconds

Convert Milliseconds to Minutes and Seconds in java using methods like toMinutes() and toSeconds(), TimeUnit which is in the concurrent package.

Milliseconds:

1 millisecond = 0.001 second or (1/1000) seconds

Seconds: 

1 second = 1000 millisecond
1 second = (1/60) minutes

Minute:

1 minute = 60000 milliseconds
1 minute = 60 seconds
1 minute = (1/60)hour

Example:

Input : Milliseconds = 400000
Output: 6 minutes and 40 seconds

Input : Milliseconds = 5400000
Output: 90 minutes and 0 seconds

Algorithm:

  1. Take Input in milliseconds.
  2. Convert Milliseconds to minutes using the formula:  minutes = (milliseconds/1000)/60).
  3. Convert Milliseconds to seconds using the formula: seconds = (milliseconds/1000)%60).
  4. The print output from Milliseconds to minutes and seconds

Methods of Solving: Java Program to Convert Milliseconds to Minutes and Seconds.

  1. Simply using Basic Input / Output in Java.
  2. Using Methods in Java.

1. Program to Convert Milliseconds to Minutes and Seconds

Java




// Java Program to Convert Milliseconds
// to Minutes and Seconds
 
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        // Take Input in Long otherwise
        // overflow occur for some inputs.
        long milliseconds = 3500000;
 
        // formula for conversion for
        // milliseconds to minutes.
        long minutes = (milliseconds / 1000) / 60;
 
        // formula for conversion for
        // milliseconds to seconds
        long seconds = (milliseconds / 1000) % 60;
 
        // Print the output
        System.out.println(milliseconds + " Milliseconds = "
                           + minutes + " minutes and "
                           + seconds + " seconds.");
    }
}


Output

3500000 Milliseconds = 58 minutes and 20 seconds.

Time complexity: O(1)
Auxiliary space: O(1)

2. Program to Convert Milliseconds to Minutes and Seconds using Methods.

Java




// Java Program to Convert Milliseconds
// to Minutes and Seconds
 
import java.io.*;
import java.util.concurrent.TimeUnit;
 
class GFG {
    public static void main(String[] args)
    {
        long milliseconds = 3500000;
 
        // This method uses this formula :minutes =
        // (milliseconds / 1000) / 60;
        long minutes
            = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
 
        // This method uses this formula seconds =
        // (milliseconds / 1000);
        long seconds
            = (TimeUnit.MILLISECONDS.toSeconds(milliseconds)
               % 60);
 
        // Print the answer
        System.out.format(milliseconds + " Milliseconds = "
                          + minutes + " minutes and "
                          + seconds + " seconds");
    }
}


Output

3500000 Milliseconds = 58 minutes and 20 seconds

Time complexity: O(1)
Auxiliary space: O(1)

 

RELATED ARTICLES

Most Popular

Recent Comments