Calculating dates given two dates excluding weekend

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

    Neatly using Java 8 Streams:

      public LocalDate[] filterWeekdaysForRange(final LocalDate start,final LocalDate end) {
        return Stream.iterate(start, date -> date.plusDays(1))
                .limit(ChronoUnit.DAYS.between(start, end)+1)
                .filter(d->d.getDayOfWeek() != SATURDAY)
                .filter(d->d.getDayOfWeek() != SUNDAY)
                .toArray(LocalDate[]::new);
      }
    

    This is an adaption of a solution provided here: https://stackoverflow.com/a/38220748/744133

    0 讨论(0)
提交回复
热议问题