0

This algorithm is pretty popular, but in some cases i do get some conflict with TimeUnit results.

   long millis = 12884983;
System.out.println(((millis / (1000 * 60)) % 60));
System.out.println(java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes(millis));

Prints:
34
214

JBoy
  • 5,398
  • 13
  • 61
  • 101

1 Answers1

1

First line is wrong:

System.out.println(((millis / (1000 * 60)) % 60));

and should be

System.out.println((millis / (1000 * 60)));

The mod operation cuts off your result. If you calculate 214 % 60 you get 34.

Cfx
  • 2,272
  • 2
  • 15
  • 21
  • thx, this should be edited then: http://stackoverflow.com/questions/10874048/from-milliseconds-to-hour-minutes-seconds-and-milliseconds – JBoy Oct 31 '14 at 09:58
  • No, the this is still true: http://stackoverflow.com/questions/10874048/from-milliseconds-to-hour-minutes-seconds-and-milliseconds I think you got confused about the representation what you want. 214 Minutes is 3 hours and 34 minutes.... – Cfx Oct 31 '14 at 10:01
  • 12884983ms cann be expressed as 214 minutes OR as 3 hours and 34 minuts which is exacly the same time span. You need to decide which way you want to express the time span. `java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes` is just doing the first. – Cfx Oct 31 '14 at 15:02