本人微信公众号"aeolian"~

秒转为时分秒格式

将seconds转为hh:mm:ss

    /**
     * 秒转hh:mm:ss格式
     * @param secString 秒字符串
     * @return hh小时mm分ss秒
     * String
     */
    public static String secondsToFormat(String secString){
        Integer seconds = Integer.parseInt(secString);
        Integer hour =0;
        Integer min =0;
        Integer second =0;
        String result ="";
        
        if (seconds>60) {   //是否大于零
            min = seconds/60;  //分钟
            second = seconds%60;  //
            if (min>60) {   //存在时
                hour=min/60;
                min=min%60;
            }
        }
        if (hour>0) {
            result=hour+"小时";
        }
        if (min>0) {
            result=result+min+"分";
        }else if (min==0&&hour>0) {  //当分为0时,但是时有值,所以要显示,避免出现2时0秒现象
            result=result+min+"分";
        }
        result=result+second+"秒";   //秒必须出现无论是否大于零
        System.out.println(result);
        return result;
    }

调用

    public static void main(String[] args) {
        secondsToFormat("7210");
    }

《秒转为时分秒格式》

 

点赞

Leave a Reply

Your email address will not be published. Required fields are marked *