Start with the start date. Then add the interval to it, until you reach the end date.
Example:
public static Iterator<LocalDateTime> datesBetween(LocalDateTime start, LocalDateTime end, int periodInMinutes) {
return new DatesBetweenIterator(start, end, periodInMinutes);
}
private static class DatesBetweenIterator implements Iterator<LocalDateTime> {
private LocalDateTime nextDate;
private final LocalDateTime end;
private final int periodInMinutes;
private DatesBetweenIterator(LocalDateTime start, LocalDateTime end, int periodInMinutes) {
this.nextDate = start;
this.end = end;
this.periodInMinutes = periodInMinutes;
}
@Override
public boolean hasNext() {
return nextDate.isBefore(end);
}
@Override
public LocalDateTime next() {
LocalDateTime toReturn = nextDate;
nextDate = nextDate.plusMinutes(periodInMinutes);
return toReturn;
}
}