JBoy
JBoy

Reputation: 5745

Conversion algorithm milliseconds to minutes correct?

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

Upvotes: 0

Views: 350

Answers (1)

Cfx
Cfx

Reputation: 2312

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.

Upvotes: 1

Related Questions