Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
333 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...