This should do it, though it's a little bit "brute-force-ish".
I assumed your non-work days were Saturday and Sunday.
I also assumed that the 2 dates you're passing in are relatively close to each other. For instance, if there were 10 years between the 2 dates, then this approach might be too inefficient.
In that case, I would lop off the beginning days until the first Monday, lop off the end days after the last Sunday, and use (genericDays % 7) * 5 (Five workdays per Seven weekdays) to figure out how many workdays are in the middle. Then I would still have to use the below code to add the workdays that occurred in the days that I lopped off at the start and finish.
Hope this helps,
Dave Seligson
SCJP, SCWCD
public static class WorkingDays {
public static int getWorkDays(Date startDt, Date endDt) {
Calendar startCal,endCal;
startCal = Calendar.getInstance();
startCal.setTime(startDt);
endCal = Calendar.getInstance();
endCal.setTime(endDt);
int workDays = 0;
//Return 0 if start and end are the same
if (startCal.getTimeInMillis()==endCal.getTimeInMillis()) {
return 0;
}
//Just in case the dates were transposed this prevents infinite loop
if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
startCal.setTime(endDt);
endCal.setTime(startDt);
}
do {
startCal.add(Calendar.DAY_OF_MONTH, 1);
if (startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SATURDAY && startCal.get(Calendar.DAY_OF_WEEK) != Calendar.SUNDAY) {
++workDays;
}
} while (startCal.getTimeInMillis() < endCal.getTimeInMillis() );
return workDays;
}
}