2
public class App {

    public static void main(String[] args) {
        final String cobDate = "2021-04-08";
        final String recordDate = "2020-12-01";
    }

    public static ZonedDateTime getDate(String date, DateTimeFormatter formatter) {
        LocalDate localDate = LocalDate.parse(date, formatter);
        return localDate.atStartOfDay(ZoneId.of(ZoneOffset.UTC.getId()));
    }
}

I am using java 8 and I want to check that the age of recordDate is greater than 5 days or not. But not from today but 5 days older than cobDate? How can I implement this and perhaps by using the already existing utility method I have that returns a ZoneDateTime? It should exclude weekends (e.g. Saturday & Sunday) and only consider business days.

Some Scenarios:

cobDate: 08/04/2021 recordDate: 08/04/2021 ==> false >> not older than 5 days

cobDate: 08/04/2021 recordDate: 31/03/2021 ==> true>> older than 5 days

cobDate: 08/04/2021 recordDate: 02/04/2021 ==> false >> not older than 5 days

Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
M06H
  • 1,675
  • 3
  • 36
  • 76
  • 3
    "only consider business days" - would holidays need to be excluded as well? If not, calculating the number of days between 2 dates and subtracting the number of saturdays and sundays should be fairly straight forward using the java.time api – Thomas Apr 08 '21 at 10:14
  • @Thomas for simplicity, only weekends for now. – M06H Apr 08 '21 at 10:16
  • I've added an answer to the "duplicate" question which you might want to check out. There is no need to iterate although if you're only interested in "older than 5 days or not" you could simply iterate backwards starting at `cobDate` and count business days until you hit a value > 5 or `recordDate`. If record date is years in the past there's no need to iterate to know it's been 100s or 1000s of days since then (even if you need to know that there's no need to iterate). – Thomas Apr 08 '21 at 12:12

1 Answers1

2
private static long countBusinessDaysBetween(LocalDate startDate, LocalDate endDate) 
    {
        if (startDate == null || endDate == null) {
            throw new IllegalArgumentException("Invalid method argument(s) to countBusinessDaysBetween(" + startDate
                    + "," + endDate);
        }

        if (startDate.isAfter(endDate)) {
           throw new IllegalArgumentException("Start Date must be before End Date");
        }
 
        Predicate<LocalDate> isWeekend = date -> date.getDayOfWeek() == DayOfWeek.SATURDAY
                || date.getDayOfWeek() == DayOfWeek.SUNDAY;
 
        long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
 
        long businessDays = Stream.iterate(startDate, date -> date.plusDays(1)).limit(daysBetween)
                .filter(isWeekend.negate()).count();
        return businessDays;
    }

From a very good article where I have just removed holidays check and added a constraint that endDate must always be after startDate

calculate business days

Then you can just do

public boolean isOlderThanXDays(int xDays, LocalDate startDate, LocalDate endDate) {
      return (YourClassName.countBusinessDaysBetween(startDate, endDate) > xDays)
  }
Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47