Package fc.cron

Class CronExpression


  • public class CronExpression
    extends Object
    This provides cron support for java8 using java-time.

    Parser for unix-like cron expressions: Cron expressions allow specifying combinations of criteria for time such as: "Each Monday-Friday at 08:00" or "Every last friday of the month at 01:30"

    A cron expressions consists of 5 or 6 mandatory fields (seconds may be omitted) separated by space.
    These are:

    Field   Allowable values   Special Characters
    Seconds (may be omitted)   0-59   , - * /
    Minutes   0-59   , - * /
    Hours   0-23   , - * /
    Day of month   1-31   , - * ? / L W
    Month   1-12 or JAN-DEC (note: english abbreviations)   , - * /
    Day of week   1-7 or MON-SUN (note: english abbreviations)   , - * ? / L #

    '*' Can be used in all fields and means 'for all values'. E.g. "*" in minutes, means 'for all minutes'

    '?' Can be used in Day-of-month and Day-of-week fields. Used to signify 'no special value'. It is used when one want to specify something for one of those two fields, but not the other.

    '-' Used to specify a time interval. E.g. "10-12" in Hours field means 'for hours 10, 11 and 12'

    ',' Used to specify multiple values for a field. E.g. "MON,WED,FRI" in Day-of-week field means "for monday, wednesday and friday"

    '/' Used to specify increments. E.g. "0/15" in Seconds field means "for seconds 0, 15, 30, ad 45". And "5/15" in seconds field means "for seconds 5, 20, 35, and 50". If '*' s specified before '/' it is the same as saying it starts at 0. For every field there's a list of values that can be turned on or off. For Seconds and Minutes these range from 0-59. For Hours from 0 to 23, For Day-of-month it's 1 to 31, For Months 1 to 12. "/" character helps turn some of these values back on. Thus "7/6" in Months field specify just Month 7. It doesn't turn on every 6 month following, since cron fields never roll over

    'L' Can be used on Day-of-month and Day-of-week fields. It signifies last day of the set of allowed values. In Day-of-month field it's the last day of the month (e.g.. 31 jan, 28 feb (29 in leap years), 31 march, etc.). In Day-of-week field it's Sunday. If there's a prefix, this will be subtracted (5L in Day-of-month means 5 days before last day of Month: 26 jan, 23 feb, etc.)

    'W' Can be specified in Day-of-Month field. It specifies closest weekday (monday-friday). Holidays are not accounted for. "15W" in Day-of-Month field means 'closest weekday to 15 i in given month'. If the 15th is a Saturday, it gives Friday. If 15th is a Sunday, then it gives following Monday.

    '#' Can be used in Day-of-Week field. For example: "5#3" means 'third friday in month' (day 5 = friday, #3 - the third). If the day does not exist (e.g. "5#5" - 5th friday of month) and there aren't 5 fridays in the month, then it won't match until the next month with 5 fridays.

    Case-sensitive No fields are case-sensitive

    Dependencies between fields Fields are always evaluated independently, but the expression doesn't match until the constraints of each field are met. Overlap of intervals are not allowed. That is: for Day-of-week field "FRI-MON" is invalid,but "FRI-SUN,MON" is valid

    • Constructor Detail

      • CronExpression

        public CronExpression​(String expr)
      • CronExpression

        public CronExpression​(String expr,
                              boolean withSeconds)
    • Method Detail

      • nextTimeAfter

        public ZonedDateTime nextTimeAfter​(ZonedDateTime afterTime)
                                    throws IllegalArgumentException
        Will search for the next time within the next 4 years. If there is no time matching, an IllegalArgumentException will be thrown (it is very likely that the cron expression is invalid, like the February 30th).
        Parameters:
        afterTime - the start search time
        Returns:
        the next time
        Throws:
        IllegalArgumentException - if no next time available in a 4 year limit
      • nextLocalDateTimeAfter

        public LocalDateTime nextLocalDateTimeAfter​(LocalDateTime dateTime)
                                             throws IllegalArgumentException
        Will search for the next time within the next 4 years. If there is no time matching, an IllegalArgumentException will be thrown (it is very likely that the cron expression is invalid, like the February 30th).
        Parameters:
        dateTime - the start search time
        Returns:
        the next time
        Throws:
        IllegalArgumentException - if no next time available in a 4 year limit
      • nextTimeAfter

        public ZonedDateTime nextTimeAfter​(ZonedDateTime afterTime,
                                           long durationInMillis)
                                    throws IllegalArgumentException
        will search for the next time within the next durationInMillis millisecond. Be aware that the duration is specified in millis, but in fact the limit is checked on a day-to-day basis.
        Parameters:
        afterTime - the start search time
        durationInMillis - the time barrier inclusive
        Returns:
        the next time after the given time, before or equal the time barrier
        Throws:
        IllegalArgumentException
      • nextTimeAfter

        public ZonedDateTime nextTimeAfter​(ZonedDateTime afterTime,
                                           ZonedDateTime dateTimeBarrier)
                                    throws IllegalArgumentException
        will search for the next time within the next dateTimeBarrier. If there is no time matching, an IllegalArgumentException will be thrown (it is very likely that the cron expression is invalid, like the February 30th).
        Parameters:
        afterTime - the start search time
        dateTimeBarrier - the date time barrier inclusive
        Returns:
        the next time after the given time, before or equal the time barrier
        Throws:
        IllegalArgumentException