recurring event logic

后端 未结 3 2105
北荒
北荒 2021-01-02 02:40

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

3条回答
  •  臣服心动
    2021-01-02 03:12

    I know nothing about Groovy, and my first suggestion was going to be Joda, but you know about it.

    I know this may seem overkill for you, and maybe even not applicable, but Quartz Scheduler handles all this recurrence and events related rules pretty well. You could not use its scheduling capabilities, and just use the Trigger classes (like CronTrigger) to calculate the event dates for you.

    The CronTrigger link above shows some examples of expressions you could use to handle your events, like this particularly nasty situation:

    "0 0 12 L * ?" - trigger an event at mid day every last day of the month (no headaches with leap years and such)

    Daylight saving time issues are handled as well.

    As for the code, create the trigger with the desired recurrence and then you may extract all the firing times you wish:

    Date firstFireTime = myTrigger.getNextFireTime();
    ...
    while (...) {
        Date nextFireTime = myTrigger.getFireTimeAfter(previousFireTime);
        ...
    }
    

    Hope this can be useful.

提交回复
热议问题