Converting Milliseconds to Minutes and Seconds?

user2426902 picture user2426902 · Jul 12, 2013 · Viewed 223.4k times · Source

I have looked through previous questions, but none had the answer I was looking for. How do I convert milliseconds from a StopWatch method to Minutes and Seconds? I have:

 watch.start();

to start the stopwatch and

  watch.stop();

to stop the watch. I later have

  watch.getTime();

which returns Milliseconds. I want it to return in Seconds and Minutes. How do I go about doing so? I'm looking for a way to do it without multiplying/dividing by 1000 but rather a method that will make the whole computation more readable and less error-prone.

Answer

Avi picture Avi · Jul 13, 2013

I would suggest using TimeUnit. You can use it like this:

long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);