Calculating dates given two dates excluding weekend

前端 未结 7 562
予麋鹿
予麋鹿 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:16

    To improve upon what @samir-machado-de-oliveira posted, here is a function that will calculate days sans weekends without using a loop. I have not benchmarked this against the loop version, but it appears as though it would be faster:

    /**
     * Gets number of days between two dates. Ignoring weekends.
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDaysBetweenIgnoreWeekends(DateTime startDate, DateTime endDate) {
        // If the start date is equal to the closing date, spent 0 days
        if (startDate.equals(endDate))
            return 0;
    
        // A number that represents the day for the start date, Monday = 1 , Tuesday = 2 , Wednesday = 3 ...
        int dayOfWeekStartDateNumber = startDate.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());
        }
    
        // How many days have passed counting weekends
        int days = Days.daysBetween(startDate, endDate).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;
    }
    

    Also, here is a version that will allow you to ignore the time of the day, so that if the start date is at 11AM on the start time and the end time is the next day at 10AM it will show as 1 day instead of 0 days.

    /**
     * Gets number of days between two dates. Ignoring weekends. Ignores Hours.
     * @param startDate
     * @param endDate
     * @return
     */
    public static int getDaysBetweenIgnoreWeekends(DateTime startDate, DateTime endDate) {
        return getDaysBetweenIgnoreWeekends(startDate,endDate,true);
    }
    
    
    
    /**
     * Gets number of days between two dates. Ignoring weekends.
     * @param startDate
     * @param endDate
     * @param ignoreTimeOfDay
     * @return
     */
    public static int getDaysBetweenIgnoreWeekends(DateTime startDate, 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();
    
        // 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());
        }
    
        // How many days have passed counting weekends
        int days;
        if(ignoreTimeOfDay) {
            days = Days.daysBetween(startDate.toLocalDate(), endDate.toLocalDate()).getDays();
        } else {
            days = Days.daysBetween(startDate, endDate).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;
    }
    

提交回复
热议问题