Find your content:

Search form

You are here

Method to check whether a date is a Holiday


Tried a lot to write a method to check whether a day is a holiday. I want an Apex class/method to check whether a day is holiday according to a particular business hour that is defined in the system. It should dynamically consider all the holidays that are entered in the business hour. Please suggest a way to check the same.

Attribution to: Avidev9

Possible Suggestion/Solution #1

Public static boolean isHoliday(Date checkDate) {
    BusinessHours bt = [SELECT Id,MondayStartDate FROM BusinessHours 
        WHERE isDefault = true];
    Datetime checkDatetime = Datetime.newInstance(checkDate.year(), checkDate.month(),, 
        bt.MondayStartDate.hour(), bt.MondayStartDate.minute(), bt.MondayStartDate.seconds());
    return !BusinessHours.isWithin(bt.Id,checkDateTime);

the method isWithin(businessHoursId, targetDate) return true if the inserted datetime is a working day, based on the BusinessHours passed (Apex Developer Guide)

In this case, I'm assuming that the work starts at the same hour for each working day of the week.

Attribution to: Christian Croce

Possible Suggestion/Solution #2

I have an apex class that does something like this.

It uses the businesshour.add method. Basically, if you add one millisecond to and it's still the same date, then you know that you are within the business hours (otherwise, that second would kick you into the next day when you re-open). If you don't specify one, or it's not there, it'll default to your org default business hours.

So to use this to find holidays, set up a business hours (org settings) where you're 24/7 but with some holidays. Mine is testing that we ARE open, so based on your question you might want to switch the true/false condition to return true when it IS a holiday.

public static boolean isItBusinessHours (string bhname){
    Businesshours bh = new Businesshours();
         bh = [select id from businesshours where name =: bhname];
    } catch (exception e){
        bh = [select id from businesshours where isdefault= true];
    return (businesshours.add(,, 1).day();

Attribution to: Shane McLaughlin

Possible Suggestion/Solution #3

I basically did the same thing but instead of check for same date I added 5 seconds to now and checked to see if it was less than the next business hour second.

datetime now =; 

BusinessHours bh = [select id from BusinessHours where Name = 'your business hour name'];
Datetime check = BusinessHours.add(, now, 1000L); 

if(now.addSeconds(5) < check) {
    system.debug('outside bus hours');

Attribution to: ITMANHAH
This content is remixed from stackoverflow or stackexchange. Please visit

My Block Status

My Block Content