import java.time.temporal.ChronoUnit;
import java.time.*;
import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    private static final int MIN_DAYS_INTERVAL = 7;

    public static void main(String[] args) {
        var holidays = List.of(
                LocalDate.of(2023, 7, 17),
                LocalDate.of(2023, 7, 18),
                LocalDate.of(2023, 7, 20)
        );

        var list = List.of(
                LocalDate.of(2023, 7, 7),
                LocalDate.of(2023, 7, 8),
                LocalDate.of(2023, 7, 9),
                LocalDate.of(2023, 7, 10),
                LocalDate.of(2023, 7, 11),
                LocalDate.of(2023, 7, 12),
                LocalDate.of(2023, 7, 13),
                LocalDate.of(2023, 7, 14),
                LocalDate.of(2023, 7, 15),
                LocalDate.of(2023, 7, 16),
                LocalDate.of(2023, 7, 17)
        );

        var executionDate1 = LocalDate.of(2023, 7, 17);
        System.out.println(String.format("Execution date: %s", executionDate1));
        System.out.println(Arrays.toString(list.stream().filter(item -> shouldSendWhatsapp(executionDate1, item, holidays)).toArray()));

        var executionDate2 = LocalDate.of(2023, 7, 19);
        System.out.println(String.format("\nExecution date: %s", executionDate2));
        System.out.println(Arrays.toString(list.stream().filter(item -> shouldSendWhatsapp(executionDate2, item, holidays)).toArray()));

        var executionDate3 = LocalDate.of(2023, 7, 24);
        System.out.println(String.format("\nExecution date: %s", executionDate3));
        System.out.println(Arrays.toString(list.stream().filter(item -> shouldSendWhatsapp(executionDate3, item, holidays)).toArray()));
    }


    private static boolean shouldSendWhatsapp(LocalDate executionDate, LocalDate insuranceCreationDate, List<LocalDate> holidays) {
        if (executionDate == null || insuranceCreationDate == null) return false;

        if (executionDate.isBefore(insuranceCreationDate)) return false;

        if (executionDate.getDayOfWeek() == DayOfWeek.SATURDAY || executionDate.getDayOfWeek() == DayOfWeek.SUNDAY || holidays.contains(executionDate))
            return false;

        var restDaysCount = recursiveRestDayCheck(executionDate.minusDays(1), holidays, 1);

        for (var i = 0; i < restDaysCount; i++) {
            var daysBetween = ChronoUnit.DAYS.between(insuranceCreationDate, executionDate.minusDays(i));

            if (daysBetween >= MIN_DAYS_INTERVAL && daysBetween % MIN_DAYS_INTERVAL == 0) return true;
        }

        return false;
    }

    private static int recursiveRestDayCheck(LocalDate dateToCheck, List<LocalDate> holidays, int checkCounts) {
        var dayOfWeek = dateToCheck.getDayOfWeek();

        if ((dayOfWeek == DayOfWeek.SATURDAY || dayOfWeek == DayOfWeek.SUNDAY || holidays.contains(dateToCheck)) && checkCounts < 100) {
            return recursiveRestDayCheck(dateToCheck.minusDays(1), holidays, checkCounts + 1);
        }
        return checkCounts;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: