How to convert the seconds in this format “HH:mm:ss”

前端 未结 6 696
栀梦
栀梦 2021-02-06 23:31

I want to convert the second/milliseconds in this format \"HH:mm:ss\" (for esamples, from 5 seconds to 00:00:05). I tried to get that format in this way:

int mil         


        
6条回答
  •  忘掉有多难
    2021-02-07 00:02

    Java 9 answer:

        Duration dur = Duration.ofMillis(millis);
        System.out.println(String.format("%02d:%02d:%02d",
                           dur.toHours(), dur.toMinutesPart(), dur.toSecondsPart()));
    

    (not tested yet)

    The toXxPart methods are described in the API docs for Java 9.

提交回复
热议问题