Class CronExpression

  • All Implemented Interfaces:
    java.lang.Comparable<CronExpression>

    public class CronExpression
    extends java.lang.Object
    implements java.lang.Comparable<CronExpression>
    Schedule class represents a parsed crontab expression.

    The schedule class cannot be instantiated using a constructor, a Schedule object can be obtain by using the static create(java.lang.String) method, which parses a crontab expression and creates a Schedule object.

    Original version https://github.com/asahaf/javacron

    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      int compareTo​(CronExpression anotherCronExpression)
      Compare two Schedule objects based on next occurrence.
      static CronExpression create​(java.lang.String expression)
      Parses crontab expression and create a Schedule object representing that expression.
      boolean equals​(java.lang.Object obj)
      Compares this object against the specified object.
      java.lang.String getExpression()  
      int getNumberOfFields()  
      int hashCode()  
      static boolean isLeapYear​(int year)  
      java.time.Instant next()
      Calculates the next occurrence based on the current time (at UTC TimeZone).
      java.time.Instant next​(java.time.Instant baseInstant, java.time.ZoneId zoneId)
      Calculates the next occurrence based on provided base time.
      java.time.Instant next​(java.time.ZoneId zoneId)
      Calculates the next occurrence based on the current time (at the given TimeZone).
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • create

        public static CronExpression create​(java.lang.String expression)
        Parses crontab expression and create a Schedule object representing that expression.

        The expression string can be 5 fields expression for minutes resolution.

          ┌───────────── minute (0 - 59)
          │ ┌───────────── hour (0 - 23)
          │ │ ┌───────────── day of the month (1 - 31)
          │ │ │ ┌───────────── month (1 - 12 or Jan/January - Dec/December)
          │ │ │ │ ┌───────────── day of the week (0 - 6 or Sun/Sunday - Sat/Saturday)
          │ │ │ │ │
          │ │ │ │ │
          │ │ │ │ │
         "* * * * *"
         

        or 6 fields expression for higher, seconds resolution.

          ┌───────────── second (0 - 59)
          │ ┌───────────── minute (0 - 59)
          │ │ ┌───────────── hour (0 - 23)
          │ │ │ ┌───────────── day of the month (1 - 31)
          │ │ │ │ ┌───────────── month (1 - 12 or Jan/January - Dec/December)
          │ │ │ │ │ ┌───────────── day of the week (0 - 6 or Sun/Sunday - Sat/Saturday)
          │ │ │ │ │ │
          │ │ │ │ │ │
          │ │ │ │ │ │
         "* * * * * *"
         
        Parameters:
        expression - a crontab expression string used to create Schedule.
        Returns:
        Schedule object created based on the supplied crontab expression.
        Throws:
        InvalidCronExpressionException - if the provided crontab expression is invalid. The crontab expression is considered invalid if it is not properly formed, like empty string or contains less than 5 fields or more than 6 field. It's also invalid if the values in a field are beyond the allowed values range of that field. Non-occurring schedules like "0 0 30 2 *" is considered invalid too, as Feb never has 30 days and a schedule like this never occurs.
      • next

        public java.time.Instant next()
        Calculates the next occurrence based on the current time (at UTC TimeZone).
        Returns:
        Instant of the next occurrence.
      • next

        public java.time.Instant next​(java.time.ZoneId zoneId)
        Calculates the next occurrence based on the current time (at the given TimeZone).
        Returns:
        Instant of the next occurrence.
      • next

        public java.time.Instant next​(java.time.Instant baseInstant,
                                      java.time.ZoneId zoneId)
        Calculates the next occurrence based on provided base time.
        Parameters:
        baseInstant - Instant object based on which calculating the next occurrence.
        Returns:
        Instant of the next occurrence.
      • compareTo

        public int compareTo​(CronExpression anotherCronExpression)
        Compare two Schedule objects based on next occurrence.

        The next occurrences are calculated based on the current time.

        Specified by:
        compareTo in interface java.lang.Comparable<CronExpression>
        Parameters:
        anotherCronExpression - the Schedule to be compared.
        Returns:
        the value 0 if this Schedule next occurrence is equal to the argument Schedule next occurrence; a value less than 0 if this Schedule next occurrence is before the argument Schedule next occurrence; and a value greater than 0 if this Schedule next occurrence is after the argument Schedule next occurrence.
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares this object against the specified object. The result is true if and only if the argument is not null and is a Schedule object that whose seconds, minutes, hours, days, months, and days of weeks sets are equal to those of this schedule.

        The expression string used to create the schedule is not considered, as two different expressions may produce same schedules.

        Overrides:
        equals in class java.lang.Object
        Parameters:
        obj - the object to compare with
        Returns:
        true if the objects are the same; false otherwise
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • isLeapYear

        public static boolean isLeapYear​(int year)
      • getNumberOfFields

        public int getNumberOfFields()
      • getExpression

        public java.lang.String getExpression()