毫秒数转换成天时分秒、时分秒

2020-05-15  本文已影响0人  恋君一生
1、毫秒数转换成天时分秒
    /**
     * 毫秒数转换成天时分秒
     *
     * @param milliseconds
     */
    private void millisecondsConvertToDHMS(long milliseconds) {
        String daysStr;
        String hoursStr;
        String minutesStr;
        String secondsStr;
        //天
        long day = (milliseconds / 1000) / (24 * 3600);
        if (day < 10) {
            daysStr = "0" + day;
        } else {
            daysStr = day + "";
        }
        //时
        long hour = ((milliseconds / 1000) % (24 * 3600)) / 3600;
        if (hour < 10) {
            hoursStr = "0" + hour;
        } else {
            hoursStr = hour + "";
        }
        //分
        long minute = ((milliseconds / 1000) % 3600) / 60;
        if (minute < 10) {
            minutesStr = "0" + minute;
        } else {
            minutesStr = minute + "";
        }
        //秒
        long second = (milliseconds / 1000) % 60;
        if (second < 10) {
            secondsStr = "0" + second;
        } else {
            secondsStr = second + "";
        }
    }
2、毫秒数转换成时分秒
    /**
     * 毫秒数转换成时分秒
     *
     * @param milliseconds
     */
    private void millisecondsConvertToHMS(long milliseconds) {
        String hoursStr;
        String minutesStr;
        String secondsStr;

        //时
        long hour = (milliseconds / 1000) / 3600;
        if (hour < 10) {
            hoursStr = "0" + hour;
        } else {
            hoursStr = hour + "";
        }
        //分
        long minute = ((milliseconds / 1000) % 3600) / 60;
        if (minute < 10) {
            minutesStr = "0" + minute;
        } else {
            minutesStr = minute + "";
        }
        //秒
        long second = (milliseconds / 1000) % 60;
        if (second < 10) {
            secondsStr = "0" + second;
        } else {
            secondsStr = second + "";
        }
    }
上一篇下一篇

猜你喜欢

热点阅读