I'm trying to use APEX BusinessHours
class to check whether a certain Date
is a Business Day
or Not.
Here is my Business Hour
in my Organization:
Based on this, if I provide following date input, it doesn't return correct value of whether my provided date is a Business Day or not:
date inputDate = date.newInstance(2018, 2, 2); //2nd Feb, 2018 = FRIDAY
datetime inputDateGmt = datetime.newInstanceGmt(inputDate, Time.newInstance(0, 0, 0, 0));
system.debug('@@... inputDate: ' + inputDate);
system.debug('@@... inputDateGmt: ' + inputDateGmt);
boolean isBusinessDay = BusinessHours.isWithin('01m90000000KlnrAAC', inputDateGmt);
system.debug('@@... isBusinessDay: ' + isBusinessDay);
But my isBusinessDay variable returning True, given that 2-Feb-2018 is a Friday and Is Not a Business Day based on my Business Hours. The Timezone of my logged in user (Timezone: 'Asia/Kolkata') is different than BusinessHours Timezone.
Do we need to match the Timezone of Local User to the Business Hour Timezone? If so how do we check if a Day is business day or not for any user with different timezone using the APEX api function?
== Update 1 ==
My debugging output with datetime.newInstance()
method call:
@@ ... inputDate: 2018-02-02 00:00:00
@@ ... inputDateGmt: 2018-02-02 00:00:00
@@ ... isBusinessDay: true
My debugging output with datetime.newInstanceGmt()
method call:
@@ ... inputDate: 2018-02-02 00:00:00
@@ ... inputDateGmt: 2018-02-01 18:30:00
@@ ... isBusinessDay: true
And notice that, isBusinessDay
is TRUE
both times.
== Update 2 ==
When I update my inputDateGmt
variable to datetime inputDateGmt = datetime.newInstanceGmt(inputDate, time.newInstance(8, 0,0,0));
, then only I get isBusinessDay
= FALSE
.