0

Converting a long millisecond value in to the format 00:00:00.000. I can't have hours wrapping around. So I cant' use DateFormat.

long millis = 3600000;
String hms = String.format("%02d:%02d:%02d",  TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1));
//Output: 01:00:00

This code excludes milliseconds.

  • 7
    Why so complicated? Just use Javas Dateformat-API. Simple, fast and no need to reinvent the wheel. –  May 17 '16 at 20:58
  • http://stackoverflow.com/a/625624/2958086 – Compass May 17 '16 at 21:00
  • @Paul It doesn't help me, If I use dateformat it I choose to display it in a 24hours format (HH) and the the time is past noon, the milliseconds equivalent to 1hour will display as 13. If I put it as a 12hours format (hh), and the milliseconds are enough for 14hours or so, it will show as 2hours. Compass Either of those codes will return it the way I need, most they do is exactly what I already did on the code I posted. – Marcel Caferati May 17 '16 at 23:42
  • @MarcelCaferati I don't quite get what your problem is. `HH:mm:ss.SSS` as format-string works just fine for me. Matches your format, correct time, everything's just the way it's supposed to be... . As for the thing with the hours: I don't exactly get where the problem would be with a 24hr-format behaving the way it's supposed to be (and same for 12hr-format)? If it's just the issue with 24:01 and comparable formats, just use `k` instead of `H` for the hours (see the [docs](http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html) ) –  May 17 '16 at 23:56
  • @Paul I also tried that already, the problem is that dateformat corrects the time based on real world hours based on the day. But my problem is that the milliseconds I am using have absolutely no relation to the day, it is simply a lap time. Example: "the racer took 1 hour (3600000) milliseconds for that lap" I will be adding to that time the penalty "1hour penalty" so his time will be a total of 2hours. I am not saying there is a problem with the api. I am just assuming it doesn't work for that purpose. BUT I will give it another try. Thank you – Marcel Caferati May 18 '16 at 02:07
  • @MarcelCaferati So you want the hours to not wrap around? Well, that'd have been quite useful info in the question in the first place. In that case: where's the problem with proceeding with your approach and simply including the milliseconds as well using the same pattern? –  May 18 '16 at 09:07
  • @Paul I tried using the same code yea, but I can't understand how it works, I can't simply add another "%03d" and put another parameter on that "string.format" in order to get the milliseconds remaining from the last "%" . It won't accept – Marcel Caferati May 18 '16 at 11:35

1 Answers1

1

You can try this code:

long millis = 3600000;
String hms = String.format("%02d:%02d:%02d.%03d",  TimeUnit.MILLISECONDS.toHours(millis),
TimeUnit.MILLISECONDS.toMinutes(millis) % TimeUnit.HOURS.toMinutes(1),
TimeUnit.MILLISECONDS.toSeconds(millis) % TimeUnit.MINUTES.toSeconds(1),
    millis % 1000);

It extends the format-string by ".%03d" and adds the milliseconds (millis % 1000) to the parameter-list. Tested on java v1.8.0_91 (Oracle JRE).