[Feature Request] Shift cron expression by Duration
Author: nem0-97Created Jan 4, 2025Updated Jul 27, 2026
Labelsis:feature
Description:
Sometimes are given Cron expression defining when to trigger an action from a separate client/component, but you may want to trigger earlier so can perform required work first.
From what I can tell this currently is not supported with Cron expressions to specify an offset so can trigger offset from original expression?
For scenarios were this is required would be useful to have a utility that takes a Cron expression and a Duration(+/-) and either/both:
- Generates a set of new cron expressions that can achieve desired trigger effects
- needs to be set as sometimes Cron expressions when shifted by Duration no longer can be represented as single Cron expression? Or just error if asked to shift and can no longer be single expression?
- e.g. Cron expression for every 5th of the month with offset of minus a 7 day duration can't be mapped to single new Cron expression
- needs to be set as sometimes Cron expressions when shifted by Duration no longer can be represented as single Cron expression? Or just error if asked to shift and can no longer be single expression?
- Allows iterating over the trigger times Cron expression represents but offsets each value returning by the provided duration
It also may be useful to allow for providing a duration to use as offset (+/-) for the Calendar used to calculate cron trigger times.
Thoughts:
- If allow specifying Duration (+/-) when creating CronExpression maybe can either: a. Use Java Calendar's roll/add methods to add that duration (broken into components) to the Calendar used for calculations?: - https://github.com/quartz-scheduler/quartz/blob/main/quartz/src/main/java/org/quartz/CronExpression.java#L1184 - https://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html b. Convert calculated Date's to Instants apply the Duration offset and convert back to Date? - https://github.com/quartz-scheduler/quartz/blob/main/quartz/src/main/java/org/quartz/CronExpression.java#L343 - https://docs.oracle.com/javase/8/docs/api/java/util/Date.html - https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html
- For calculating new Cron expression given original one and a Duration would be probably a lot more work, but could be more flexible and allow 1 time transformation when setting things up and passing expression to other systems that end up doing the triggering.
Source: quartz-scheduler/quartz