编程语言
首页 > 编程语言> > 【Java】时间转换(可用于计算耗时场景)

【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
    }
}

 

标签:00,场景,Java,TimeCalc,long,60,耗时,timeNumber,calc
来源: https://www.cnblogs.com/danhuai/p/16095337.html