I have the following method that I took from the accepted answer this question Calculate number of weekdays between two dates in Java
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
Calendar startCal = Calendar.getInstance();
startCal.setTime(startDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(endDate);
int workDays = 0;
//Return 0 if start and end are the same
if (startCal.getTimeInMillis() == endCal.getTimeInMillis()) {
return 0;
}
if (startCal.getTimeInMillis() > endCal.getTimeInMillis()) {
startCal.setTime(endDate);
endCal.setTime(startDate);
}
do {
//excluding start date
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()); //excluding end date
return workDays;
}
I pass to that function the first day and the last day of the current month I get the days like this:
Calendar firstDayOfMonth = Calendar.getInstance();
firstDayOfMonth .set(Calendar.DAY_OF_MONTH,
Calendar.getInstance().getActualMinimum(Calendar.DAY_OF_MONTH));
Calendar lastDayOfMonth = Calendar.getInstance();
lastDayOfMonth .set(Calendar.DAY_OF_MONTH,
Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH));
and I pass the parameters to the function like this:
getWorkingDaysBetweenTwoDates(firstDayOfMonth.getTime(),
lastDayOfMonth.getTime());
I try the method and is returning 21 and we are in November of 2016 and this month have 22 working days not 21
I printed in console the parameters and these are the paramaters that I'm passing to the method
firstDayOfMonth.getTime() //equals to this Tue Nov 01 09:09:47 VET 2016
lastDayOfMonth.getTime() //equals to this Wed Nov 30 09:09:47 VET 2016