I have a value in milliseconds which I would like to covert to HH::MM:SS.fff This is just for duration purposes.
I know there is a basic way of doing this:
String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes(millis),
TimeUnit.MILLISECONDS.toSeconds(millis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millis))
);
But is there a better way of doing this? Thanks.
This math will do the trick :
int sec = (int)(millis/ 1000) % 60 ;
int min = (int)((millis/ (1000*60)) % 60);
int hr = (int)((millis/ (1000*60*60)) % 24);
If you want only Minute and Second, Then :
int sec = (int)(millis/ 1000) % 60 ;
int min = (int)((millis/ (1000) / 60);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With