Business Hours Calculation with Apex


In Salesforce, managing your organization’s business hours is crucial for smooth operations. While Salesforce provides a BusinessHours class in Apex, it offers only a basic set of methods. For more intricate business hours calculations, customizing the logic may be necessary. In this blog, we’ll explore how to create a custom Apex class to handle business days, ensuring it aligns with your specific requirements. Let’s dive into how you can set this up, with a little help from Tenetizer Technologies.

Setting Up Business Hours

Step 1: Go to your Salesforce setup. You can find it by clicking on your profile picture and selecting “Setup.”

Step 2: In the Setup menu, under “Company Profile,” click on “Business Hours.”

This feature is incredibly versatile, allowing you to work in specific time zones and locations for various Salesforce functionalities, including cases, case escalation rules, and case milestones in entitlement processes.

The apex code

					public class BusinessDays {
  private List<Boolean> businessDay = new Boolean[7];
  private List<Time> startHours = new Time[7];
  private List<Time> endHours = new Time[7];
  private Date knownSunday = Date.newInstance(2013, 1, 6);

  // Constructor creates businessDay array
  public BusinessDays() {
    BusinessHours bh = [Select SundayStartTime, MondayStartTime, TuesdayStartTime,
                        WednesdayStartTime, ThursdayStartTime, FridayStartTime,
                        SaturdayStartTime, SundayEndTime, MondayEndTime, TuesdayEndTime,
                        WednesdayEndTime, ThursdayEndTime, FridayEndTime, SaturdayEndTime
                      From BusinessHours 
                      Where IsDefault=true];

    businessDay[0] = (bh.SundayStartTime != null);
    // ... (repeat for other days of the week)

    startHours[0] = bh.SundayStartTime;
    // ... (repeat for other days of the week)

    endHours[0] = bh.SundayEndTime;
    // ... (repeat for other days of the week)

  // Check if today is a business day
  public Boolean isBusinessDay(Date inputDate) {
    Integer i = Math.mod(Math.abs(this.knownSunday.daysBetween(inputDate)), 7);
    return businessDay[i];

  // Get the start time
  public Time getStartTime(Date inputDate) {
    Integer i = Math.mod(Math.abs(this.knownSunday.daysBetween(inputDate.date)), 7);
    return startHours[i];

  // Gets next business day, skipping non-business days
  public Date nextBusinessDay(Datetime inputDatetime) {
    Integer i = Math.mod(Math.abs(this.knownSunday.daysBetween(inputDatetime.date())), 7);
    Datetime returnDate = inputDatetime;
    while (!businessDay[Math.mod(i, 7)]) {
      returnDate = returnDate.addDays(1);
    return returnDate.date;

Undersating the code

This Apex class, named BusinessDays, facilitates business hours calculations. Let’s break down the key components:

  • Arrays for Business Days, Start Times, and End Times: Three arrays are created to store information about the operational days, start times, and end times.
  • Handling Locale Differences: The knownSunday variable ensures that the start of the week is correctly identified, accounting for potential differences in locales.
  • Methods for Business Day Calculations:

    isBusinessDay(Date inputDate): Checks if a given date falls within a business day.

    getStartTime(Date inputDate): Retrieves the start time for a given date.

    nextBusinessDay(Datetime inputDatetime): Finds the next business day, considering non-business days.

Implementing Business Logic

With this custom Apex class, you can now implement specific business logic tailored to your organization’s unique requirements. For instance:

Checking if Open for Business Today:

					if (busDays.isBusinessDay(Date.today())) {
  // ...


Determining Opening Hours:

					if (busDays.getStartTime(Date.today())) {
  // ...


Finding the Next Business Day:

					Datetime nextBusDay = busDays.nextBusinessDay(Datetime.now());



Salesforce’s BusinessHours class provides a foundation for handling business hours, but for more specialized operations, custom Apex code may be necessary. By creating a custom class like the one demonstrated here, you can fine-tune business hours calculations to align with your organization’s unique needs. For these requirements turn to Tenetizer Technologies. Our expertise in Salesforce customization and optimization ensures your business hours are precisely what you need them to be.