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

前端 未结 6 599
栀梦
栀梦 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:00

    I wrote a simple utility function for this task which does not require any Java version nor instantiates any unnecessary objects:

    /**
     * provides a String representation of the given time
     * @return {@code millis} in hh:mm:ss format
     */
    public static final String formatTime(long millis) {
        long secs = millis / 1000;
        return String.format("%02d:%02d:%02d", secs / 3600, (secs % 3600) / 60, secs % 60);
    }
    

    Unlike some other solutions here, this can even deal with up to 100 hours

提交回复
热议问题