Hi,
I'm working on a Groovy/Java calendar-type application that allows the user to enter events with a start date and an optional recurrence. If it's a recurring event, it may recurr:
- monthly on a date of the month that corresponds to the start date
- weekly on a day of the week of that corresponds to the start date
- every 2 weeks on a day of the week of that corresponds to the start date
- etc.
I originally planned on using the Google calendar API to do all the recurrence logic, but it proved to be an enormous PITA, for reasons I'll discuss further if anyone cares.
So now, I've decided to roll my own solution. Given a date, I want to figure out whether a recurring event occurs on this date. My logic (in pseudocode) will be as follows:
public boolean occursOnDate(def date, def event) {
def firstDate = event.startDate
if (firstDate > date) {
return false;
} else if (event.isWeekly()) {
return event.dayOfWeek() == date.dayOfWeek()
} else if (event.isMonthly()) {
return event.dayOfMonth() == date.dayOfMonth()
} else {
// At this point we know the event occurs every X weeks where X > 1
// Increment firstDate by adding X weeks to it as many times as possible, without
// going past date
return firstDate == date
}
}
This logic seems reasonable, but will actually be quite a lot of effort to implement when you consider all the weird edge cases (e.g. how to handle a monthly recurring event in February whose first occurrence is Jan 31).
Is there a library that can take help me to implement this? Some specifics would be much appreciated (e.g. no credit will be awarded for "Use Joda Time").
Thanks, Don