Write a Java Program to Convert Milliseconds to Minutes and Seconds

As a Java programmer, you may come across situations where you need to convert milliseconds into minutes and seconds.

This process is straightforward, and you can easily do it by using simple arithmetic operations in Java.

Here is a Java program that converts milliseconds to minutes and seconds:

import java.util.Scanner;

public class MillisecondsConverter {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter time in milliseconds: ");
        long milliseconds = sc.nextLong();
        long minutes = (milliseconds / 1000) / 60;
        long seconds = (milliseconds / 1000) %% 60;
        System.out.println(milliseconds + " milliseconds = " + minutes + " minutes and " + seconds + " seconds.");
    }
}

In this program, we have first imported the java.util.Scanner package to take input from the user.

Then, we have created a class named MillisecondsConverter.

Inside the main method, we have created a new instance of the Scanner class to read input from the user.

We have then printed a message asking the user to enter the time in milliseconds.

Next, we have declared a long variable named milliseconds to store the input value.

We have then used simple arithmetic operations to convert the input milliseconds to minutes and seconds.

We have used the formula (milliseconds / 1000) / 60 to convert milliseconds to minutes.

This formula first divides the input milliseconds by 1000 to get the time in seconds and then divides it by 60 to get the time in minutes.

Similarly, we have used the formula (milliseconds / 1000) % 60 to get the remaining seconds after converting the milliseconds to minutes.

This formula first divides the input milliseconds by 1000 to get the time in seconds and then takes the remainder when it is divided by 60.

Finally, we have printed the output by concatenating the values of milliseconds, minutes, and seconds using the + operator.

That’s it!

This is a simple Java program to convert milliseconds to minutes and seconds.

You can use this program to quickly convert time values in your Java applications.