5

I have the three field like below and I want to calculate the business hours but need to reduce the hour if the exist the holiday.

I have write this code into the apex class and call this class form the trigger after update.

 BusinessHours bh = [SELECT id  From BusinessHours 
   Where IsDefault=true];

Datetime inTime = custom date time field 1
Datetime outTime = Custom date time field 2;

long businessMillisecondsDiff = BusinessHours.diff(bh.Id, inTime, outTime);
decimal businessHoursDiff = businessMillisecondsDiff / (1000.0*60.0*60.0);

custom_text_field__c = businessHoursDiff + 'Hours';

Office hour : 9 to 5 pm

Output :

intime = 01/01/2013 9:00 AM

out time = 03/01/2013 10:00 PM

Note; 2/1/2103 is holiday

Expected output is : 16 hours

My question is :

If we added the holiday by using the setup, how to reduce that holiday hours?
Thanks

5
  • What type of Datetime ranges are you expecting to have? Hours, Days, Months, Years between the in and out times? There is the BusinessHours.isWithin(string, Datetime) method, which will include holidays in the calculation. However, it will only tell you is a certain point in time is a holiday. Commented Aug 1, 2014 at 10:37
  • what exactly you mean by "how to reduce the holiday hours ?" with your example, are you setting up the holiday on 1/1/2013 and then trying to find the difference ? Commented Aug 1, 2014 at 10:37
  • Amended the question @Vamsi Krishna Commented Aug 1, 2014 at 10:45
  • did you try running your code in Developer Console ? the whole point of calling the diff method with a businessHourId is to calculate the difference based on the business hours considering holidays as well. Commented Aug 1, 2014 at 11:33
  • 1
    I'm voting to close this question as off-topic because too old to migrate. Commented Jun 15, 2016 at 11:28

2 Answers 2

2
+100

Pleasee use following code:

public with sharing class time_calculation {

    public static String segment_text(String segment_string, Integer segment_integer, String prior_segments) {
        String return_string; //string for returning
        String spacer = ''; //string for holding an additional spacer
        if (segment_string != 'Second') { //if the segment being determined is not Seconds
            spacer = ' '; //create a spacer value
        }
        if (segment_integer > 1) { //if the value is greater than 1
            return_string = segment_integer.format()+' '+segment_string+'s'+spacer; //format
        } else if (segment_integer > 0) { //if the value is greater than 0
            return_string = segment_integer.format()+' '+segment_string+spacer; //format
        } else { //otherwise
            if (prior_segments != '' || segment_string == 'Second') { //if there is a value for prior segments or this is the seconds segment
                return_string = '0 '+segment_string+'s'+spacer; //format
            } else {
                return_string = ''; //set variable to null
            }
        }
        return return_string; //pass back the string
    }

    public static String duration_between_two_date_times(DateTime start_date_time, DateTime end_date_time) {
        Integer start_year_as_int = start_date_time.year(); //grab the start year
        Integer start_day_as_int = start_date_time.dayOfYear(); //grab the start day
        Integer start_hour_as_int = start_date_time.hour(); //grab the start hour
        Integer start_minute_as_int = start_date_time.minute(); //grab the start minute
        Integer start_second_as_int = start_date_time.second(); //grab the start second
        Integer start_in_seconds = (start_year_as_int * 31556926) + (start_day_as_int * 86400) + (start_hour_as_int * 3600) + (start_minute_as_int * 60) + (start_second_as_int * 1); //convert the start date to a value in seconds
        //there are 31556926 seconds in one year and that is why we are mutiplying the start_year_as_int value by 31556926 > this same logic applies to the days, hours & minutes logic which is why there are weird multipliers in that line of code
        Integer end_year_as_int = end_date_time.year(); //grab the end year
        Integer end_day_as_int = end_date_time.dayOfYear(); //grab the end day
        Integer end_hour_as_int = end_date_time.hour(); //grab the end hour
        Integer end_minute_as_int = end_date_time.minute(); //grab the end minute
        Integer end_second_as_int = end_date_time.second(); //grab the end second
        Integer end_in_seconds = (end_year_as_int * 31556926) + (end_day_as_int * 86400) + (end_hour_as_int * 3600) + (end_minute_as_int * 60) + (end_second_as_int * 1); //convert the end date to a value in seconds
        Integer total_duration_in_seconds = end_in_seconds - start_in_seconds; //duration in seconds
        Integer year_result = math.mod(math.floor(total_duration_in_seconds/31556926).intValue(),10000000); //number of years
        Integer day_result = math.mod(math.floor(total_duration_in_seconds/86400).intValue(),365); //number of days
        Integer hour_result = math.mod(math.floor(total_duration_in_seconds/3600).intValue(),24); //number of hours
        Integer minute_result = math.mod(math.floor(total_duration_in_seconds/60).intValue(),60); //number of minutes
        Integer second_result = math.mod(math.floor(total_duration_in_seconds/1).intValue(),60); //number of seconds

        String year_text_string = segment_text('Year', year_result, ''); //string variable for text regarding Year
        String day_text_string = segment_text('Day', day_result, year_text_string); //string variable for text regarding Day
        String hour_text_string = segment_text('Hour', hour_result, year_text_string + day_text_string); //string variable for text regarding Hour
        String minute_text_string = segment_text('Minute', minute_result, year_text_string + day_text_string + hour_text_string); //string variable for text regarding Minute
        String second_text_string = segment_text('Second', second_result, year_text_string + day_text_string + hour_text_string + minute_text_string); //string variable for text regarding Second
        String return_string = year_text_string + day_text_string + hour_text_string + minute_text_string + second_text_string;//concatenate all the strings into one for our resutling test string
        return return_string; //pass back the final string
    }

}
1

Please use below the code to find.

  public Integer calculateWorkingDaysBetweenTwoDates(Date date1,Date date2){

               List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];

                Integer allDaysBetween = date1.daysBetween(date2);
                Integer allWorkingDays=0;
                for(Integer k=0;k<allDaysBetween ;k++ ){
                    if(checkifItisWorkingDay(date1.addDays(k),holidays)){
                        allWorkingDays++;
                    } 
                }

                return allWorkingDays;

      }

public boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                 Date weekStart  = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                        if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                                 return false;
                        }
                }
               if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                       return false;
                } else 
                       return true;
  }

You must log in to answer this question.

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