將秒數轉換成天具體的天時分秒

/**
 * 將秒數轉換成天具體的天時分秒
 * 比如172800S轉換成2000秒
 * @param second
 * @return
 */
public String formatSecond(Object second){
        String  timeStr = "0秒";
        if(second!=null){
            Double s=(Double) second;
            String format;
            Object[] array;
            Integer days = (int)(s /(60 * 60 * 24));
            Integer hours =(int)(s/(60*60) - days * 24);
            Integer minutes = (int)(s/60 - hours*60 - days * 24 * 60);
            Integer seconds = (int)(s-minutes*60-hours*60*60 - days * 24 * 60 * 60);
            if(days>0){
                format="%1$,d天%2$,d時%3$,d分%4$,d秒";
                array=new Object[]{days,hours,minutes,seconds};
            } else if(hours>0){
                format="%1$,d時%2$,d分%3$,d秒";
                array=new Object[]{hours,minutes,seconds};
            }else if(minutes>0){
                format="%1$,d分%2$,d秒";
                array=new Object[]{minutes,seconds};
            }else{
                format="%1$,d秒";
                array=new Object[]{seconds};
            }
            timeStr = String.format(format, array);
        }
        return timeStr ;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章