天天看點

java 秒轉時分秒

java 秒轉時分秒

/**
     * 秒轉時分秒
     * @param second
     * @return
     */
    public static String getFormatTime(Integer second) {

        if (second != null) {
            String num0 = NumFormat(0);
            if(second < 60) {//秒
                return num0 + ":"+ num0 + ":" + NumFormat(second);
            }

            if(second < 3600) {//分

                return num0 + ":" +NumFormat(second / 60) + ":" + NumFormat(second % 60);
            }

            if(second< 3600 * 24) {//時

                return NumFormat(second/ 60 / 60) + ":" + NumFormat(second/ 60 % 60) + ":" + NumFormat(second% 60);
            }

            if(second>= 3600 * 24) {//天

                return NumFormat(second/ 60 / 60 /24) + "天" +NumFormat(second/ 60 / 60 % 24) + ":" + NumFormat(second/ 60 % 60) + ":" + NumFormat(second% 60);
            }
        }

        return "--";
    }

    /**
     * 格式化時間
     * @param sec
     * @return
     */
    private static String NumFormat(int sec) {
        if (String.valueOf(sec).length() < 2){
            return "0"+sec;
        }else {
            return String.valueOf(sec);
        }
    }