Calculate Weekdays between Two Dates

This example shows how you can use Groovy to calculate the number of weekdays between two dates.

// Global Function getWorkingDaysBetweenTwoDates
// Returns: Long
// Parameters: ActivityStartDate (Date), ActivityEndDate (Date)

Calendar startCal = Calendar.getInstance();
startCal.setTime(ActivityStartDate);
Calendar endCal = Calendar.getInstance();
endCal.setTime(ActivityEndDate);
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(ActivityEndDate);
  endCal.setTime(ActivityStartDate);
}
//excluding end date
while(startCal.getTimeInMillis() < endCal.getTimeInMillis()){
  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;
  }
}
return workDays;