function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Adriana Reyes 26Adriana Reyes 26 

Help determining if a date is a holiday using Apex


So I'm trying to figure out how to tell if the date entered in a date field falls on a holiday in Apex. I've been using the isWithin() method in the BusinessHours object to tell if a date is not within business hours, but the requirement has recently changed to go even further. The reason behind this is because we charge different rates on holidays. For example, Saturday would normally be considered "after hours" rates but if Saturday is Christmas Day, then we need a way to mark that in a checkbox or something so we can bill a holiday rate.

Any help is appreciated. Thank you!
PriyaPriya (Salesforce Developers) 
Hey Adriana,

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();

If it helps, please mark it as best answer