【Java】时间转换(可用于计算耗时场景)

 

public class TimeCalc {
    /**
     * 将指定数字转换为时间(格式:  00:00:00,000)
     *
     * @param timeNumber 如 216059999
     * @return 如 60:00:59,999
     */
    public static String calc(long timeNumber) {
        long hUnit = 60 * 60 * 1000;
        long mUnit = 60 * 1000;
        long sUnit = 1000;
        long h = (long) timeNumber / hUnit;
        long m = (long) (timeNumber % hUnit) / mUnit;
        long s = (long) ((timeNumber % hUnit) % mUnit) / sUnit;
        long ms = ((timeNumber % hUnit) % mUnit) % sUnit;
        String calcResult = String.format(
                "%02d:%02d:%02d,%03d",
                h,
                m,
                s,
                ms
        );
        return calcResult;
    }

    public static void main(String[] args) {
        System.out.println(TimeCalc.calc(3599999)); // 00:59:59,999
        System.out.println(TimeCalc.calc(216059999)); // 60:00:59,999
        System.out.println(TimeCalc.calc(0)); // 00:00:00,000
        System.out.println(TimeCalc.calc(360012000)); // 100:00:12,000
    }
}

 

posted @ 2022-04-03 10:50  淡怀  阅读(243)  评论(0编辑  收藏  举报