I have two dates example: start date = Friday 03/19/2020 and end date = Tuesday 03/21/2020. If I return the number of calendar days, it will be 4. How can I write the method to only return the business days from the above start and end date?
Thanks!
I have two dates example: start date = Friday 03/19/2020 and end date = Tuesday 03/21/2020. If I return the number of calendar days, it will be 4. How can I write the method to only return the business days from the above start and end date?
Thanks!
 
    
    Here is one possibility, there may be others that are more concise.
Using the following dates as starting and ending points.
        LocalDate start = LocalDate.of(2020, 1, 1);
        LocalDate end = LocalDate.of(2020,4,18);
Now define a Predicate to ignore Saturdays and Sundays.  This will be applied to the date stream generated below.
        Predicate<LocalDate> onlyWeekDays = ld -> !ld.getDayOfWeek()
                .equals(DayOfWeek.SATURDAY) 
                && !ld.getDayOfWeek().equals(DayOfWeek.SUNDAY);
Now use the datesUntil method to generate a stream of dates between the starting and end points.  
        long days = start.datesUntil(end.plusDays(1)).filter(onlyWeekDays::test).count();
        System.out.println(days);
All date related methods used are from the java.time package in the Java API.
Note:  As an alternative as mentioned in the comments, an EnumSet of the DayofWeek enum would also serve as a filter.  It would be used as follows:
        Set<DayOfWeek> wkends = 
             EnumSet.of( DayOfWeek.SATURDAY,DayOfWeek.SUNDAY );
        long days = 
                start
                .datesUntil( end.plusDays(1) )
                .filter( ld -> !wkends.contains( ld.getDayOfWeek() ) )
                .count()
        ;
 
    
    