English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In the above program, you will learn how to convert milliseconds to minutes and seconds in Java.
import java.util.concurrent.TimeUnit; public class Milliseconds { public static void main(String[] args) { long milliseconds = 1000000; // long minutes = (milliseconds / 1000) / 60; long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds); // long seconds = (milliseconds / 1000); long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds); System.out.format("%d milliseconds = %d minutes ", milliseconds, minutes); System.out.println("Or"); System.out.format("%d milliseconds = %d seconds", milliseconds, seconds); } }
When running the program, the output is:
1000000 milliseconds = 16 Minutes Or 1000000 milliseconds = 1000 seconds
In the above program, we use the toMinutes() method to convert the given milliseconds to minutes. Similarly, we use the toSeconds() method to convert it to seconds
We can also use basic mathematics to convert it to minutes and seconds.
Seconds = Milliseconds / 1000
Minutes are
Minutes = Seconds / 60 or Minutes = (Milliseconds / 1000) / 60
public class Milliseconds { public static void main(String[] args) { long milliseconds = 1000000; long minutes = (milliseconds / 1000) / 60; long seconds = (milliseconds / 1000) % 60; System.out.format("%d milliseconds = %d minutes again %d seconds.", milliseconds, minutes, seconds); } }
When running the program, the output is:
1000000 milliseconds = 16 Minutes again 40 seconds.
In the above program, we use the formula:
Minutes = (Milliseconds / 1000) / 60 And Remaining Seconds = (Milliseconds / 1000) % 60
First, we simply divide it by the number of seconds, and then divide60 to calculate minutes (Minutes).
Then, we calculate the remaining seconds (Remaining Seconds), divide it by the number of seconds, and then divide60 get the remaining seconds (Remaining Seconds).