Java Program to Convert Milliseconds to Minutes and Seconds

Java Program to Convert Milliseconds to Minutes and Seconds

In this article, we will see a Java Program to Convert Milliseconds to Minutes and Seconds. As we know milliseconds, minutes and seconds are the units of time. So for converting milliseconds to minutes and seconds, we can use Timeunit package from java.util.concurrent library. In this package, we have two built-in functions for this conversion:

Java Program to Convert Milliseconds to Minutes and Seconds:
TimeUnit.MILLISECONDS.toSeconds(milliseconds)-convert milliseconds in minutes
TimeUnit.MILLISECONDS.toMinutes(milliseconds)-convert milliseconds in seconds

Both functions take argument milliseconds that you want to convert. Let’s see a program to Convert Milliseconds to Minutes and Seconds with the help of these functions :

Java program to convert milliseconds into minutes using an inbuilt function

import java.util.concurrent.TimeUnit;
public class Main
{
	public static void main(String[] args) {
	    long mills = 500000;

        long min = TimeUnit.MILLISECONDS.toMinutes(mills);
        System.out.println(mills+ " Milliseconds is equal to the "+min+" minutes");
	}
}


Output :

500000 Milliseconds is equal to the 8 minutes

Java program to convert milliseconds into seconds using an inbuilt function

import java.util.concurrent.TimeUnit;
public class Main
{
	public static void main(String[] args) {
	    long mills = 500000;
	    long seconds = TimeUnit.MILLISECONDS.toSeconds(mills);
     System.out.println(mills + " Milliseconds is equal to the " + seconds + " Seconds");
	}
}


Output :

500000 Milliseconds is equal to the 500 Seconds

Instead of using a built-in java package, we can convert milliseconds time to minutes or seconds by using a mathematical formula.

Formula to convert Milliseconds into Seconds :

seconds = (milliseconds / 1000)

Formula to convert Milliseconds into Minutes :

minutes = (milliseconds / 1000) / 60

Java Program to convert milliseconds into seconds and minutes without using an inbuilt function

import java.util.concurrent.TimeUnit;
public class Main
{
	public static void main(String[] args) {
	    long mills = 500000;
	    long seconds = (mills/1000);
        System.out.println(mills + " Milliseconds is equal to the " + seconds + " Seconds");
        long minutes = (mills/ 1000) / 60;
        System.out.println(mills + " Milliseconds is equal to the " + minutes + " minutes");
	}
}


Output :

500000 Milliseconds is equal to the 500 Seconds
500000 Milliseconds is equal to the 8 minutes

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Java Online Quiz

Level up your coding skills with our interactive programming quiz!