Calculate Business Days In Apex – Utility Class

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(, 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

Manish Choudhari

I am a certified Salesforce Application & System Architect and Developer working on Salesforce Technology since 2014. Currently, I have 14 Salesforce certifications along with OCPJP (Oracle Certified Profession JavaSE6 Programmer) working in Hyderabad as a Technical Engineer. Writing technical blogs, learning new technologies and frameworks and sharing knowledge is my hobby.

This Post Has 7 Comments

  1. please calculate days with Half day PM and Half day AM..

  2. Appreciate you sharing this utility class.

    I have been trying to write a simple test class. However, I am receiving a ‘Maximum Stack Depth’ error in the Class.BusinessDays.getPreviousWorkingDay method. The method seems straight forward but I cannot seem to get it to work. Any thoughts on what I am doing wrong?

    Here is my simple test class:
    public class BusinessDaysTest {

    static testMethod void testMethod1() {
    BusinessDays bd = new BusinessDays();

    System.debug(‘Inserting Todays Date’ +;
    Date today =;
    DateTime startTime = DateTime.newInstanceGMT(today.year(), today.month(),, 2, 2, 3);


    // Add Days
    System.debug(‘Starting Add Days Test For 10 Days’);
    DateTime response1 = bd.addDays(startTime, 10);
    System.assert(response1 != null);

    // Sub Days
    System.debug(‘Starting Subtract Days Test For 3 Days’);
    DateTime response2 = bd.subtractDays(startTime, 2);
    System.assert(response2 != null);

    // Is Working Day
    System.debug(‘Starting Working Day Test – Is Final Add Day a Workday’);
    Boolean response3 = bd.isWorkingDay(response1);
    System.assert(response3 != null);

    //Days Between
    System.debug(‘Starting to Counting Days between Final ADD Date and Subtract Date’);
    Integer response4 = bd.getBusDaysBetween(response1,;
    System.assert(response4 != null);


  3. Hi,

    I am not able to view BusinessDays Utility Class on the Post. Is it removed?

  4. Hi Manish,
    This is very useful post but my scenario is to calculate the business hours and check calculated business hour is lies under business timing.
    I use below method :
    BusinessHours.add(, Date, timeInterval);
    and it works !!

Leave a Reply to Stewart McNaught Cancel reply

Close Menu