两种方法,请自行参考使用
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 + "分钟";
}
/**
* 求两个时间相差的时间单位
* @param smallDate 小时间
* @param bigDate 大时间
* @param type 分别为 Calendar.DATE,Calendar.HOUR,Calendar.MINUTE
* @return 相差数量
*/
public static long minusTime(Date smallDate,Date bigDate,int type){
if(smallDate==null||bigDate==null){
return Integer.MAX_VALUE;
}
long diffMillis = Math.abs(bigDate.getTime() - smallDate.getTime());
switch (type){
case Calendar.DATE: return TimeUnit.DAYS.convert(diffMillis, TimeUnit.MILLISECONDS);
case Calendar.HOUR:return TimeUnit.HOURS.convert(diffMillis, TimeUnit.MILLISECONDS);
case Calendar.MINUTE:return TimeUnit.MINUTES.convert(diffMillis, TimeUnit.MILLISECONDS);
default:return Integer.MAX_VALUE;
}
}
网友评论