Java Program to Convert Milliseconds to Minutes and Seconds

Java Program to Convert Milliseconds to Minutes and Seconds

What is a Timeout class?

In this Article, we will write a Program to Convert Milliseconds to Minutes and Seconds in Java.
The TimeUnit class in Java is a part of the java.util.concurrent package and provides various time-based utility methods to convert and manipulate time units. It provides methods to perform various time-based operations, such as conversion between different units of time, adding or subtracting time intervals, and sleeping for a specific amount of time.

Timeout Class:

The TimeUnit class has various static methods such as toSeconds(), toMinutes(), toHours(), and toDays() that can be used to convert time from one unit to another. It also has methods like sleep(), timedJoin(), and timedWait() to pause the execution of a thread for a specific duration of time.

Java Program to Convert Milliseconds to Minutes and Seconds Using Inbuilt Function:

Run
// Importing all the Required Packages
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class Main{
    public static void main(String[] args){
        // Scanner class for taking input
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number of milliseconds: ");
        long milliseconds = input.nextLong();

        // Converting milliseconds using inbuilt functions
        long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
        long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(minutes);

        System.out.println(milliseconds + " milliseconds is equal to " + minutes + " minute(s) and " + seconds + " second(s).");
    }
}

Input:

Enter a number of milliseconds: 1000000

Output:

1000000 milliseconds is equal to 16 minute(s) and 40 second(s).

Explanation:

In the above program, we use the TimeUnit class from the java.util.concurrent package to convert the milliseconds to minutes and seconds. We use the toMinutes() method to convert the milliseconds to minutes, and we use the toSeconds() method to convert the milliseconds to seconds. To get the remaining seconds after converting to minutes, we subtract the number of minutes converted to seconds using the toSeconds() method of the TimeUnit class.

Program to Convert Milliseconds to Minutes and Seconds:

Run
// Importing all the Required Packages
import java.util.Scanner;

public class Main{
    public static void main(String[] args) {
        // Scanner class for taking input
        Scanner input = new Scanner(System.in);
        System.out.println("Enter a number of milliseconds: ");
        long milliseconds = input.nextLong();

        // Converting milliseconds using inbuilt functions
        long totalSeconds = milliseconds / 1000;
        long minutes = totalSeconds / 60;
        long seconds = totalSeconds % 60;

        System.out.println(milliseconds + " milliseconds is equal to " + minutes + " minute(s) and " + seconds + " second(s).");
    }
}

Input:

Enter a number of milliseconds: 1000000

Output:

1000000 milliseconds is equal to 16 minute(s) and 40 second(s).

Explanation:

In the above program, we take input from the user for the number of milliseconds to be converted. Then, we divide the total number of milliseconds by 1000 to get the total number of seconds. Next, we divide the total number of seconds by 60 to get the number of minutes, and we use the remainder operator % to get the number of seconds remaining.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription