Calculate Business Days In Apex – Utility Class

It’s always a challenge to Calculate business days or working days in Apex if you are not aware of BusinessHours object in Salesforce.

  • APEX offers a very powerful object BusinessHours which can be used to get business days and exclude non-business days like weekends and holidays.
  • However, the standard methods do not serve all use cases. As a result, I have created below utility class to serve all kind of use cases and scenarios.
Use Cases
  • Add or Subtract only business days from a date.
  • Exclude holidays and weekends from your date calculations.
  • Get the next or previous working day
  • Know if a specific day is a working day or not.
  • Get the number of business days between 2 dates.
What is BusinessHours object?

Before we jump to code, let’s read about BusinessHours object. You can configure your Organization’s Business hours using this object. This is easily accessible under the setup menu. Just navigate to Setup > Business Hours

  • You can set up multiple business hours for your organization. For example, support team may work 24×7 while development team may only work during weekdays.
  • You can set up multiple business hours depending on the geographical location of your team/office. For example, US team works in EST/PST timezone where Indian team works in IST timezone.
  • You can even configure holidays and associate with your business hours. You can relate one holiday with multiple business hour records.
Sample business hours record with holidays.

Sample business hours record with holidays.

BusinessDays Utility Class

This utility class has 5 different methods to serve all kind of scenarios. Use these methods in combination to get your use case served 🙂

Method Definition

  • getNextWorkingDay – Get next working day. If the date passed to this function is a working day, this will return the same date, else it will return next working day.
  • getPreviousWorkingDay – Get previous working day. If the date passed to this function is a working day, this will return the same date, else it will return previous working day.
  • addDays – Add business days to a date.
  • subtractDays – Subtract business days from a date.
  • isWorkingDay – Return if a specific day is working day or not.
  • getNoOfBusinessDaysBetweenDates – Return number of business days between 2 dates.

BusinessDays Class



How you can use BusinessDays class?
  • Setup business hours. There is already a default entry for every org.
  • Initialize BusinessDays class.
    • Using your business hour record name – BusinessDays bd = new BusinessDays(‘your business hour record name’);
    • Using default record name –  BusinessDays bd = new BusinessDays();
  • Start using methods. Like to add 3 days in today’s date, bd.addDays(Date.today(), 3);

Please be aware this class focus more on Working Days and does not focus much on Working Hours. You can comment on this post if you have any use case where the calculation needs to happen on hours and you cannot achieve it using standard methods.

Read more on: BusinessHours Class

Also Read: SOQL Performance Improvment And Skinny Tables

(Visited 1027 Times)

3 thoughts on “Calculate Business Days In Apex – Utility Class

  1. Pingback: Calculate Business Days In Apex – Utility Class – sfdc techie – Pavan's blog

  2. Pingback: JSON to CSV Convert – Utility Class for Apex and Java – SFDCFacts

Leave a Reply

Your email address will not be published. Required fields are marked *