如何计算两个日期之间的天数,不包括周末java
嗨,我尝试使用此代码进行几天工作(包括周末),因此如何排除两个日期之间的周末?
public long getDifferenceDays(Date d1, Date d2) { long diff = d2.getTime() - d1.getTime();
long diffDays = diff / (24 * 60 * 60 * 1000);
return diffDays;
}
回答:
这对你有用
DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); Date date1 = df.parse("10/08/2013");
Date date2 = df.parse("21/08/2013");
Calendar cal1 = Calendar.getInstance();
Calendar cal2 = Calendar.getInstance();
cal1.setTime(date1);
cal2.setTime(date2);
int numberOfDays = 0;
while (cal1.before(cal2)) {
if ((Calendar.SATURDAY != cal1.get(Calendar.DAY_OF_WEEK))
&&(Calendar.SUNDAY != cal1.get(Calendar.DAY_OF_WEEK))) {
numberOfDays++;
}
cal1.add(Calendar.DATE,1);
}
System.out.println(numberOfDays);
现场演示
放出
7
以上是 如何计算两个日期之间的天数,不包括周末java 的全部内容, 来源链接: utcz.com/qa/399159.html