javaicalendarfind-occurrencesrfc5545ical4j

Calendar. Recurrent event occurrence. How to check if start date matched on pattern?


If I use google library google-rfc-2445 (to calculate occurrences)

String sRule = "RRULE:FREQ=YEARLY;COUNT=3;INTERVAL=2;BYMONTH=5;BYMONTHDAY=22,23,24,25,26,27,28;BYDAY=MO";
LocalDateIterable localDateIterable = LocalDateIteratorFactory
            .createLocalDateIterable(sRule, org.joda.time.LocalDate.now(), true);
LocalDateIterator iterator = localDateIterable.iterator();
iterator.forEachRemaining(System.out::println);

I'll get in result:

2017-07-17

2019-05-27

2021-05-24

2023-05-22

considering pattern, there are should be only 3 dates. Considering each one we can find that first one is redundant. I have no marker if start date is valid or not. And as you see first one isn't. (If I use correct start date creating iterable I would get 3 correct dates in result.)

Similar experiment with lib-recur (result almost similar with small differences)

RecurrenceRule recurrenceRule = new RecurrenceRule("FREQ=YEARLY;BYMONTHDAY=23;BYMONTH=5;COUNT=3");
RecurrenceRuleIterator it = recurrenceRule.iterator(DateTime.nowAndHere());
int maxInstances = 10; // limit instances for rules that recur forever
while (it.hasNext() && (!recurrenceRule.isInfinite() || maxInstances-- > 0)) {
    DateTime nextInstance = it.nextDateTime();
    System.out.println(nextInstance);
}

Result count is correct, but first occurrence isn't. Correctness of first occurrence depends on (if start date is correct first occurrence also would be correct)...

20170717T123104

20190527T123104

20210524T123104

How I could specify or validate start date in correct way using google-rfc-2445 or lib-recur?


Solution

  • RFC 5545 states

    The "DTSTART" property for a "VEVENT" specifies the inclusive start of the event. For recurring events, it also specifies the very first instance in the recurrence set.

    That's why lib-recur always adds the start instance.

    However, we have an open issue about that (see issue 22) and I've come to the conclusion that it's better design to iterate only the instances of the rule and add the start instances at a later stage (in RecurrenceSet to be specific). That's also what RFC 5545 seems to suggest.

    I'll take care of this and it will be "fixed" in version 0.10