How to format a duration in java? (e.g format H:MM:SS)

前端 未结 19 1530
情话喂你
情话喂你 2020-11-22 03:32

I\'d like to format a duration in seconds using a pattern like H:MM:SS. The current utilities in java are designed to format a time but not a duration.

相关标签:
19条回答
  • 2020-11-22 04:30

    using this func

    private static String strDuration(long duration) {
        int ms, s, m, h, d;
        double dec;
        double time = duration * 1.0;
    
        time = (time / 1000.0);
        dec = time % 1;
        time = time - dec;
        ms = (int)(dec * 1000);
    
        time = (time / 60.0);
        dec = time % 1;
        time = time - dec;
        s = (int)(dec * 60);
    
        time = (time / 60.0);
        dec = time % 1;
        time = time - dec;
        m = (int)(dec * 60);
    
        time = (time / 24.0);
        dec = time % 1;
        time = time - dec;
        h = (int)(dec * 24);
        
        d = (int)time;
        
        return (String.format("%d d - %02d:%02d:%02d.%03d", d, h, m, s, ms));
    }
    
    0 讨论(0)
提交回复
热议问题