//获取两个日期相差天数
public int getDiscrepantDays(Date dateStart, Date dateEnd) {
//先将小时,分,秒设置为零,防止转换造成一天的误差
Calendar calendar=Calendar.getInstance();
calendar.setTime(dateStart);
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
Date date1 = calendar.getTime();
calendar.setTime(dateEnd);
calendar.set(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),0,0,0);
Date date2 = calendar.getTime();
long day = (date2.getTime() - date1.getTime()) / (24*3600*1000);
return (int)day+1;
}
网友评论