java 计算时间差(显示形式 :如1天2小时30分钟 )

来源:互联网 发布:单片机程序烧录软件 编辑:程序博客网 时间:2024/04/28 21:48
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test {
    /** 获取两个时间的时间查 如1天2小时30分钟 */
    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return "有效期:"+day + "天" + hour + "小时" + min + "分钟";
    }
    public static void main(String[] args) throws ParseException {
         // TODO Auto-generated method stub
         SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date d1=sdf.parse("2016-07-15 10:52:10");
         Date d2=sdf.parse(sdf.format(new Date()));
         System.out.println(getDatePoor(d1,d2));
     }

}


0 0