Algorithm
-
Input:
- Accept the input value for milliseconds.
-
Calculation:
- Calculate the total seconds by dividing the milliseconds by 1000.
- Calculate the minutes by dividing the total seconds by 60.
- Calculate the remaining seconds by taking the modulo (%) of total seconds by 60.
-
Output:
- Display the calculated minutes and seconds.
Code Examples
#1 Code Example- Milliseconds to Seconds/Minutes Using toMinutes() and toSeconds()
Code -
Java Programming
import java.util.concurrent.TimeUnit;
class Main {
public static void main(String[] args) {
long milliseconds = 1000000;
// us of toSeconds()
// to convert milliseconds to minutes
long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds);
System.out.println(milliseconds + " Milliseconds = " + seconds + " Seconds");
// use of toMinutes()
// to convert milliseconds to minutes
long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds);
System.out.println(milliseconds + " Milliseconds = " + minutes + " Minutes");
}
}
Copy The Code &
Try With Live Editor
Output
1000000 Milliseconds = 1000 Seconds
Or
1000000 Milliseconds = 16 Minutes
Or
1000000 Milliseconds = 16 Minutes
#2 Code Example- Milliseconds to Seconds/Minutes Using Mathematical Formula
Code -
Java Programming
class Main {
public static void main(String[] args) {
long milliseconds = 1000000;
long seconds = (milliseconds / 1000);
System.out.println(milliseconds + " Milliseconds = " + seconds + " Seconds");
long minutes = (milliseconds / 1000) / 60;
System.out.println(milliseconds + " Milliseconds = " + minutes + " Minutes");
}
}
Copy The Code &
Try With Live Editor
Output
1000000 Milliseconds = 1000 Seconds
1000000 Milliseconds = 16 Minutes
1000000 Milliseconds = 16 Minutes
Demonstration
Java Programing Example to Convert Milliseconds to Minutes and Seconds-DevsEnv