Calculating dates given two dates excluding weekend

前端 未结 7 542
予麋鹿
予麋鹿 2020-12-09 00:53

I am using Joda time api in a Spring 3.0 project for to calculate dates. Now I have a start and end date and I want to get everyday exept weekend or Saturday or Sunday betwe

7条回答
  •  一生所求
    2020-12-09 01:26

    I have been using @Josh Maag's logic for almost a year now but recently found that it returns wrong value when endDate happens to fall on a Saturday.

    Here is the version I would like to share which considers to subtract days from endDate when it happens to be a Saturday or a Sunday.

    public static int getDaysBetweenIgnoreWeekends(org.joda.time.DateTime startDate, org.joda.time.DateTime endDate, boolean ignoreTimeOfDay) {
    
        // If the start date is equal to the closing date, spent 0 days
        if (startDate.equals(endDate))
            return 0;
        if (ignoreTimeOfDay && startDate.toLocalDate().equals(endDate.toLocalDate()))
            return 0;
    
        // A number that represents the day for the start date, Monday = 1 , Tuesday = 2 , Wednesday = 3 ...
        int dayOfWeekStartDateNumber = startDate.getDayOfWeek();
        int dayOfWeekEndDateNumber = endDate.getDayOfWeek();
    
        // If the starting date is Saturday or Sunday , pretend to be Monday
        if (dayOfWeekStartDateNumber == 6 || dayOfWeekStartDateNumber == 7) {
            int DaysToAdd = 8 - dayOfWeekStartDateNumber;
            startDate = startDate.plusDays(DaysToAdd);
            dayOfWeekStartDateNumber = Integer.valueOf(startDate.dayOfWeek().getAsString());
        }
    
        org.joda.time.DateTime effectiveEndDate = endDate; 
    
        if (dayOfWeekEndDateNumber == 6 || dayOfWeekEndDateNumber == 7) {
            effectiveEndDate = endDate.minusDays(Math.abs(5 - dayOfWeekEndDateNumber)); 
        }
    
        // How many days have passed counting weekends
        int days;
        if(ignoreTimeOfDay) {
            days = org.joda.time.Days.daysBetween(startDate.toLocalDate(), effectiveEndDate.toLocalDate()).getDays();
        } else {
            days = org.joda.time.Days.daysBetween(startDate, effectiveEndDate).getDays();
        }
    
        // How many weeks have passed
        int weeks = days / 7;
        // Excess days left. E.g. one week and three days the excess will be 3
    
        int excess = days % 7;
    
        // Excess of days spent for the weekend , then it must be removed two days
        // the final number of days
        if (excess + dayOfWeekStartDateNumber >= 6) {
            // Week count * 5 working days + excess days - the weekend that excess crossed
            return weeks * 5 + excess - 2;
        }
        // Weeks count * 5 working days + excess days
        return weeks * 5 + excess;
    }
    

    In the earlier version - following snippet was subtracting an extra day. Subtracting -2 no matter if endDate was a Saturday or Sunday.

    if (excess + dayOfWeekStartDateNumber >= 6) {
        // Week count * 5 working days + excess days - the weekend that excess crossed
        return weeks * 5 + excess - 2;
    } 
    

    Hope that helps!

提交回复
热议问题