Java program to convert milliseconds to minutes and seconds
sptth://www.theitroad.com
Here's a Java program to convert milliseconds to minutes and seconds:
import java.util.concurrent.TimeUnit; public class MillisecondsToMinutesAndSeconds { public static void main(String[] args) { long milliseconds = 60000; // 1 minute in milliseconds long minutes = TimeUnit.MILLISECONDS.toMinutes(milliseconds); long seconds = TimeUnit.MILLISECONDS.toSeconds(milliseconds) - TimeUnit.MINUTES.toSeconds(minutes); System.out.println("Milliseconds: " + milliseconds); System.out.println("Minutes: " + minutes); System.out.println("Seconds: " + seconds); } }
In this program, we have a long integer variable milliseconds
that represents the number of milliseconds we want to convert to minutes and seconds. We then use the TimeUnit
class to convert the milliseconds to minutes and seconds using the toMinutes()
and toSeconds()
methods.
We first convert the milliseconds
to minutes and store the result in a long
variable called minutes
. Then, we subtract the number of seconds in minutes
from the total number of seconds in milliseconds
using the toSeconds()
method and store the result in a long
variable called seconds
. Finally, we print out the values of milliseconds
, minutes
, and seconds
using the System.out.println()
method.