In Apex, how can you calculate the number of days between two dates excluding any days that fall on the weekend?
This would be similar to the existing Date.daysBetween() method.
There is a similar question that deals with the same issue in formula fields – Calculating Turnaround Time in Date Fields.
The question How Can I Tell the Day of the Week of a Date? provides methods for finding the day of the week in Apex.
Best Answer
So I wrote some code to handle this recently. It is completely configurable, and as efficient as can be.
The most important thing is to set the
isWorkingDay
array with the right flags (index 0 is Monday) and to make sureworkingDaysInWeek
matches the number of true flags in the array. After that the method will return a value for you.