Correct Way of Calculating Elapsed Time in Java

You may also like...

2
Leave a Reply

avatar
1 Comment threads
1 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
2 Comment authors
James CaldwellNathaniel M Recent comment authors
  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)