1

I am using Time4j to parse recurring intervals like this:

IsoRecurrence.parseTimestampIntervals("R/2019-01-01T00:00:00/P1D")

This will give me an iterator with infinite number of daily recurring instances starting from the beginning of 2019.

Is it possible to only iterate the instances between a start date and end date, let's say e.g. for June, without changing the original rule?

Basically I would like to be able to define schedules with the ISO 8601 recurrence format but only need to generate instances for a given period.

Meno Hochschild
  • 38,305
  • 7
  • 88
  • 115
Arho Huttunen
  • 806
  • 6
  • 15

1 Answers1

1

Yes, it is possible but you have to introduce your own customizable condition to stop the infinite loop or stream. Example:

@Test
public void parseInfiniteTimestampIntervals() throws ParseException {
    IsoRecurrence<TimestampInterval> intervals =
        IsoRecurrence.parseTimestampIntervals("R/2019-01-01T00:00:00/P1D");

    PlainDate start = PlainDate.of(2019, 6, 11);
    PlainDate end = PlainDate.of(2019, 6, 15);

    for (TimestampInterval interval : intervals) {
        PlainDate current = interval.getStartAsTimestamp().getCalendarDate();
        if (current.isAfterOrEqual(start)) {
            if (current.isBeforeOrEqual(end)) {
                System.out.println(interval); // or do your own stuff with the current interval
            } else {
                break; // end of infinite loop
            }
        }
    }
}

Output:

[2019-06-11T00/2019-06-12T00)
[2019-06-12T00/2019-06-13T00)
[2019-06-13T00/2019-06-14T00)
[2019-06-14T00/2019-06-15T00)
[2019-06-15T00/2019-06-16T00)

However, infinite iterating requires special care how to model the stop condition and only exist in the class IsoRecurrence because the ISO-8601-standard has explicitly allowed this option. I hope that your ISO-expression (which is to be parsed) is not too wide in range because excessive iterating over many intervals should be avoided for sake of performance.

In case you only have daily intervals when the time of day is irrelevant, I recommend to use the type DateInterval.

Meno Hochschild
  • 38,305
  • 7
  • 88
  • 115
  • 2
    Thanks for the clarification. I ended up not using `IsoRecurrence` for this just because of the wideness of the ranges, but built some rules with a combination of `DateInterval` and `DayPartitionRule` to be able to generate a series of events. – Arho Huttunen Sep 27 '19 at 07:14