1

I am able to exclude SAT & SUN between two dates, but wanted to exclude holidays also.

for holidays, I am having list of Dates which I need to subtract along with Sat & sun. Please suggest me approach. Thanks!

2
  • 1
    can u post how are you able to exclude SUN and SAT? Feb 25, 2016 at 9:34
  • @Ashish : Post what you have tried so far, it will help us to answer Feb 25, 2016 at 9:38

1 Answer 1

1

Create a set<Date> with all your holidays

Pass your start, end date and set of holidays..

public class CalculateDayBetweenTwoDates {
    public static Integer calculateDays(Date dtStartDate, Date stEndDate, set<Date> setHolidayDates){

        Integer intCount = 0;
        while(dtStartDate < stEndDate){
            dtStartDate = dtStartDate.addDays(1);            
            datetime myDate = datetime.newInstance(dtStartDate.year(), 
                                                   dtStartDate.month(),
                                                     dtStartDate.day());
            if(myDate.format('EEEE') != 'Sunday' && myDate.format('EEEE') != 'Saturday' && !setHolidayDates.contains(dtStartDate)){
                intCount++;
            }
        }
        return intCount;
    }
}

Use developer console for test

set<Date> setHolidayDates = new set<Date>();
setHolidayDates.add(Date.Today().addDays(1));
setHolidayDates.add(Date.Today().addDays(2));
system.debug('======'+CalculateDayBetweenTwoDates.calculateDays(Date.today(), Date.today().addDays(16), setHolidayDates));
2
  • 3
    Pass the Set<Date> which contains the holiday dates and check it in the if condition: if(myDate.format('EEEE') != 'Sunday' && myDate.format('EEEE') != 'Saturday' && !holidaysSet.contains(myDate) ) Feb 25, 2016 at 9:41
  • @SaumyaRanjanSatapathy Thanks I really missed this .. was looking into only holidays..
    – Ratan Paul
    Feb 25, 2016 at 10:13

You must log in to answer this question.

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