今天在项目中遇到进行计算流程单从开始到结束所花费的时间,样式是XX天XX小时XX秒,有天显示天,没有就不显示;

代码献上:

package com.project.model.work;

public class DateFormat {
    
    
    /*
     * 将毫秒转化为天时分秒毫秒
     */
    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;
 
        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
        
        StringBuffer sb = new StringBuffer();
        if(day > 0) {
            sb.append(day+"天");
        }
        if(hour > 0) {
            sb.append(hour+"小时");
        }
        if(minute > 0) {
            sb.append(minute+"分");
        }
        if(second > 0) {
            sb.append(second+"秒");
        }
        if(milliSecond > 0) {
            sb.append(milliSecond+"毫秒");
        }
        return sb.toString();
    }
    
    
    public static void main(String[] args) {
        
        System.out.println(formatTime(78640000l));;
    }

}
 

  

posted on 2018-08-23 16:01  iscys  阅读(1870)  评论(0编辑  收藏  举报