-4

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!

WJS
  • 36,363
  • 4
  • 24
  • 39
  • have you tried answers mentioned here https://stackoverflow.com/questions/20165564/calculating-days-between-two-dates-with-java – Barkha Apr 17 '20 at 20:01
  • 4
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! Also see [ask]. – Oleg Apr 17 '20 at 20:04
  • 1
    Probably you are looking for https://stackoverflow.com/questions/10854119/calculate-business-days-including-holidays – Arvind Kumar Avinash Apr 17 '20 at 20:04
  • 1
    Your example dates don't make sense. 3/19/2020 was a Thursday, 3/21/2020 was a Saturday. These dates are only 2 days apart. I'm guessing you meant 3/20/2020 to 3/24/2020 ? – NickChris Apr 17 '20 at 21:26
  • Does this answer your question? [Calculate number of weekdays between two dates in Java](https://stackoverflow.com/questions/4600034/calculate-number-of-weekdays-between-two-dates-in-java). Not all the answers there are good answers. Start by picking those that use java.time, the modern Java date and time API. – Ole V.V. Apr 18 '20 at 06:01

1 Answers1

3

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.

  • The end date is increased by 1 day to make the stream inclusive of the ending date.
  • The aforementioned filter is applied to screen out weekends.
  • And the remaining dates are simply counted.
        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()
        ;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
WJS
  • 36,363
  • 4
  • 24
  • 39
  • 2
    It’s good. I think it’s as concise as it gets. It’s also well explained, thank you. – Ole V.V. Apr 18 '20 at 06:15
  • Very good. A thought: You could use [`EnumSet::contains`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/EnumSet.html) for the predicate. – Basil Bourque Apr 18 '20 at 21:23
  • @BasilBourque Good suggestion! It took me a few minutes to figure out how it would work. Check the end of the answer. – WJS Apr 18 '20 at 22:01