8

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.

3
  • Are you looking for a specific country's holidays or something more generic?
    – Mike Chale
    Nov 7, 2012 at 20:24
  • Do you mean just a weekend, not a weekday? Nov 7, 2012 at 20:25
  • Sorry missed few part in the description. I want a 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 holiday that is entered in the Business hour.
    – Avidev9
    Nov 7, 2012 at 20:37

3 Answers 3

7

I have an apex class that does something like this.

It uses the businesshour.add method. Basically, if you add one millisecond to system.now() 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();
    try{
         bh = [select id from businesshours where name =: bhname];
    } catch (exception e){
        bh = [select id from businesshours where isdefault= true];
    } 
    return (businesshours.add(bh.id, system.now(), 1).day()==system.now().day());
}
1
  • Well I did something like this only. Will be posting the solution soon.
    – Avidev9
    Nov 16, 2012 at 11:45
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 = system.now(); 

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

if(now.addSeconds(5) < check) {
    system.debug('outside bus hours');
}
2
Public static boolean isHoliday(Date checkDate) {
    BusinessHours bt = [SELECT Id,MondayStartTime FROM BusinessHours 
        WHERE isDefault = true];
    Datetime checkDatetime = Datetime.newInstance(checkDate.year(), checkDate.month(), checkDate.day(), 
        bt.MondayStartTime.hour(), bt.MondayStartTime.minute(), bt.MondayStartTime.second());
    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.

2
  • This answer is useful, but doesn't really address holidays, which was the original intent of the question.
    – Adrian Larson
    Nov 7, 2019 at 18:00
  • It depends. If he's talking about only holidays (excluding weekends), you're right. Otherwise, in this way you can understand if the inserted day is a non-working day (weekend or holiday) using the negation. I'm sorry for my english, i know it's not perfect :D Nov 8, 2019 at 9:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .