Java 将秒数转成00:00:00格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dh_Chao/article/details/78854514
/**
 * 时间工具类
 * Created by zhh on 2017/12/20.
 */
public class DateUtil {

    /**
     * 一小时的秒数
     */
    private static final int HOUR_SECOND = 60 * 60;

    /**
     * 一分钟的秒数
     */
    private static final int MINUTE_SECOND = 60;

    /**
     * 根据秒数获取时间串
     * @param second (eg: 100s)
     * @return (eg: 00:01:40)
     */
    public static String getTimeStrBySecond(int second) {
        if (second <= 0) {

            return "00:00:00";
        }

        StringBuilder sb = new StringBuilder();
        int hours = second / HOUR_SECOND;
        if (hours > 0) {

            second -= hours * HOUR_SECOND;
        }

        int minutes = second / MINUTE_SECOND;
        if (minutes > 0) {

            second -= minutes * MINUTE_SECOND;
        }

        return (hours > 10 ? (hours + "")
                : ("0" + hours) + ":" + (minutes > 10 ? (minutes + "") : ("0" + minutes)) + ":"
                        + (second > 10 ? (second + "") : ("0" + second)));
    }
}

猜你喜欢

转载自blog.csdn.net/Dh_Chao/article/details/78854514