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
Marc FriedmanMarc Friedman 

Unexpected Apex BusinessHour Class Behavior



We are seeing odd behavior with business hours in Apex. We are setting Milestone dates for a Project by starting with the Live Date (the final Milestone), and working backwards through the list of Milestones – each of which has a “days since previous Milestone”.  


This is the relevant portion of the block of code:

milliseconds = milestoneMap.get(j).Days_Since_Previous_Milestone__c.longValue() * 86400000L * -1;


System.debug('XXXXXX About to subtract ' + milliseconds + ' from ' + milestoneDateTime + ' for ' + projectMilestone.Milestone__c);


newMidDay = BusinessHours.add(bh.Id, milestoneDateTime, milliseconds);


System.debug('XXXXXX The number of milliseconds subtracted was ' + BusinessHours.diff(, newMidDay, milestoneDateTime)); 


When we run the Debug Log, it reads “XXXXXX About to subtract -172800000 from 2010-06-22 00:00:00 for a09R0000005dlMSIAY”, which is correct, because the next Milestone is two business days earlier, and there are 172800000 milliseconds in two days (it does read that it’s going to “subtract” a negative number, but you can see how the code is actually adding the negative number).  Now, the next debug entry, which is after only one line of code (the BusinessHours.add) states “XXXXXX The number of milliseconds subtracted was 86400000”.  How could this be?  So, it’s saying “I’m about to subtract 172800000 milliseconds, but I actually subtracted 86400000.”


Our only Business Hours record has 24 hours Mon - Fri and zero hours on Sat and Sun (I've confirmed through Apex Explorer).  No holidays.  The above example is subtracting two days from Monday and arriving at Friday (when it should arrive at Thursday, and should also not contradict itself with stating it's going to subtract one number but actually subtracting another).  This is also odd, because if it was "ignoring" business hours completely, it would have subtracted two days from Monday and arrived at Sunday.  Yet, if it subtracts two days from a Thursday, it accurately arrives at Tuesday (the debug log states “I’m about to subtract 172800000 milliseconds, and I did subtract 172800000.”







Ever figure this one out?  I just noticed the same sort of thing happening.  Here's some execute anonymous code for ya that will show the problem...


SYSTEM_NO_WEEKENDS BH record is 24hrs a day, except Sat and Sun which are No Hours.



BusinessHours bh1 = [SELECT Id FROM BusinessHours WHERE Name = 'SYSTEM_NO_WEEKENDS' LIMIT 1];
Datetime startDateTime =;
Datetime endDateTime = BusinessHours.add(bh1.Id, startDateTime, -1*24*60*60*1000L);
Date endDate = BusinessHours.add(bh1.Id, startDateTime, -1*24*60*60*1000L).date();
system.debug('Starttime   '+startDateTime);
system.debug('new Datetime:  '+endDateTime);
system.debug('new Date:  '+endDate);



Now... let's say you run this code today.  The addDays(-2) will put the incoming date on a Monday.  The resulting dates and times are all on Saturday.


Get rid of the negatives on everything - run it with addDays(2) and positive 24 hours on the add method - it'll skip both Sat and Sunday and work properly.


It feels to me that BusinessHours.add should never return a datetime outside of the business hours defined in the record.... clear as day I have no hours defined for Saturday yet I receive a saturday result.


Thanks for any help.



3 years later,  and yes my finding show can only add positive values.  


Any suggestion on where to go for a workaround?