Correct Way of Calculating Elapsed Time in Java

You may also like...

Leave a Reply

2 Comments on "Correct Way of Calculating Elapsed Time in Java"

avatar
  Subscribe  
newest oldest
Notify of
Nathaniel M
Guest
Nathaniel M

public String toMinuteSeconds(){
return String.format(“%d min, %d sec”, time(TimeUnit.MINUTES),
time(TimeUnit.SECONDS) – (time(TimeUnit.MINUTES) * 60));
}

The original didn’t have the ” * 60″, so the seconds didn’t subtract correctly.

James Caldwell
Guest
James Caldwell

Also in the Basic Time Elapsed code there is TimeUnit.NANOSECONDS.toHours(difference) which should be changed to TimeUnit.NANOSECONDS.toMinutes(difference)