i need to calculate the business days between two dates.
ex : we have holiday(in USA) on july4th. so if my dates are
date1 = 07/03/2012
date2 = 07/06/2012
no of business days b/w these dates should be 1 since july4th is holiday.
i have a below method to calclulate the business days which will only counts week ends but not holidays.
is there any way to calculate holidays also….please help me on this.
public static int getWorkingDaysBetweenTwoDates(Date startDate, Date endDate) {
Calendar startCal;
Calendar endCal;
startCal = Calendar.getInstance();
startCal.setTime(startDate);
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 {
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;
}
Let’s pretend you have a list containing all the holidays, as you mentioned.
Just add a condition to your
ifcondition in yourdo-while:For simplicity’s sake, I’ve assumed
holidaycontains dates in the format identical toCalendar.DAY_OF_YEAR.