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:
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
The sort of recurrence rules you want are reasonably well specified in RFC-2445 (basically, the iCal spec). Getting the minutiae of this correct can be pretty involved. I'd suggest using the the google-rfc-2445 library for this, or another implementation of that spec like iCal4J.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With