1

Below code calculates the number of workdays to be added and if the end date falls on holidays/weekends, shift the date to the next day.
But this code is on an assumption that the start date is not on weekends/holidays.

I want the code which also works if the start date falls on a weekend/holiday.

Please note this code was posted by ElenaSofea on 17 Jun'13 but as I was not able to comment on it asking this as a new question.
Reference: How to add a number of days to a Date while skipping weekends and other holidays

static DateTime CalculateFutureDate(DateTime fromDate, int numberofWorkDays,   
                                ICollection<DateTime> holidays)
{
    var futureDate = fromDate;
    for (var i = 0; i < numberofWorkDays; i++ )
    {
        if (futureDate.DayOfWeek == DayOfWeek.Saturday 
         || futureDate.DayOfWeek == DayOfWeek.Sunday
         || (holidays != null && holidays.Contains(futureDate)))
        {
            futureDate = futureDate.AddDays(1);
            numberofWorkDays++;
        }
        else
        {
            futureDate = futureDate.AddDays(1);
        }
    }
    while(futureDate.DayOfWeek == DayOfWeek.Saturday 
         || futureDate.DayOfWeek == DayOfWeek.Sunday
         || (holidays != null && holidays.Contains(futureDate)))
    {
        futureDate = futureDate.AddDays(1);
    }
    return futureDate;
}
Community
  • 1
  • 1
bhargav
  • 267
  • 3
  • 8
  • 17

2 Answers2

0

Try to pass the date as input, such that this method validates to check whether it is saturday or sunday

public static boolean isValidateSundayorSaturday(String date) throws Exception {

        Calendar calendar = Calendar.getInstance();     
        if (date != null) {

            calendar.setTime(getDate(date,null));
            int day = calendar.get(Calendar.DAY_OF_WEEK);

            if (day == Calendar.SUNDAY || day == Calendar.SATURDAY) {
                return true;
            } 
        }
        return false;
    }
Paul Richter
  • 10,908
  • 10
  • 52
  • 85
Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76
0

You've to provide all excluded dates because it is different from region to region even in same country.

If you want to see code here you go

Community
  • 1
  • 1
rana
  • 145
  • 1
  • 2
  • 8