-2

Tried the below code

header 1 header 2
cell 1
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class EventDateGenerator {
    public static void main(String[] args) {
        String input = "Occurs every 2 weeks from 28 Aug 2023 till 28 Oct 2023 on friday and monday";
        List<LocalDate> eventDates = generateEventDates(input);
        
        for (LocalDate date : eventDates) {
            System.out.println(date.format(DateTimeFormatter.ofPattern("dd MMM yyyy")));
        }
    }

    public static List<LocalDate> generateEventDates(String input) {
        List<LocalDate> eventDates = new ArrayList<>();

        String[] parts = input.split(" ");
        int interval = Integer.parseInt(parts[3]);
        
        LocalDate startDate = LocalDate.parse(parts[5] + " " + parts[6] + " " + parts[7], DateTimeFormatter.ofPattern("dd MMM yyyy"));
        LocalDate endDate = LocalDate.parse(parts[11] + " " + parts[12] + " " + parts[13], DateTimeFormatter.ofPattern("dd MMM yyyy"));
        
        String[] daysOfWeek = parts[16].split("and");

        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusWeeks(interval)) {
            for (String dayOfWeek : daysOfWeek) {
                DayOfWeek targetDay = DayOfWeek.valueOf(dayOfWeek.trim().toUpperCase());
                if (date.getDayOfWeek() == targetDay) {
                    eventDates.add(date);
                    break;
                }
            }
        }

        return eventDates;
    }
}

| | cell 3 | cell 4 |

I get a input from ui for event as "Occurs every 3 days from 28 Aug 2023 till 28 Oct 2023" I want all the dates that needs for this event

0 Answers0