Top Description Fields Constructors Methods
org.springframework.scheduling.support

public final Class CronExpression

extends Object
Class Inheritance
Imports
java.time.temporal.ChronoUnit, .Temporal, java.util.Arrays, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

Representation of a crontab expression that can calculate the next time it matches.

CronExpression instances are created through parse(String); the next match is determined with next(Temporal).

Supports a Quartz day-of-month/week field with an L/# expression. Follows common cron conventions in every other respect, including 0-6 for SUN-SAT (plus 7 for SUN as well). Note that Quartz deviates from the day-of-week convention in cron through 1-7 for SUN-SAT whereas Spring strictly follows cron even in combination with the optional Quartz-specific L/# expressions.

Author
Arjen Poutsma
Since
5.3
See Also
CronTrigger

Field Summary

Modifier and TypeField and Description
private final String
private final CronField[]
private static final String[]
pack-priv static final int

Constructor Summary

AccessConstructor and Description
private
CronExpression(CronField seconds, CronField minutes, CronField hours, CronField daysOfMonth, CronField months, CronField daysOfWeek, String expression)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
other
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public static boolean

Returns:

true if the given expression is a valid cron expression
isValidExpression
(String
the expression to evaluate
expression
)

Determine whether the given string represents a valid cron expression.

public <
the type of temporal
T extends Temporal & Comparable<? super T>
>
T

Returns:

the next temporal that matches this expression, or null if no such temporal can be found
next
(T
the seed value
temporal
)

Calculate the next Temporal that matches this expression.

private <T extends Temporal & Comparable<? super T>> T
nextOrSame(T temporal)

private <T extends Temporal & Comparable<? super T>> T
nextOrSameInternal(T temporal)

public static CronExpression

Returns:

the parsed CronExpression object
parse
(String
the expression string to parse
expression
)

Parse the given crontab expression string into a CronExpression.

private static String
resolveMacros(String expression)

public String
toString()

Overrides java.lang.Object.toString.

Return the expression string used to create this CronExpression.
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

expressionback to summary
private final String expression
fieldsback to summary
private final CronField[] fields
MACROSback to summary
private static final String[] MACROS
MAX_ATTEMPTSback to summary
pack-priv static final int MAX_ATTEMPTS

Constructor Detail

CronExpressionback to summary
private CronExpression(CronField seconds, CronField minutes, CronField hours, CronField daysOfMonth, CronField months, CronField daysOfWeek, String expression)

Method Detail

equalsback to summary
public boolean equals(Object other)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
other:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isValidExpressionback to summary
public static boolean isValidExpression(String expression)

Determine whether the given string represents a valid cron expression.

Parameters
expression:String

the expression to evaluate

Returns:boolean

true if the given expression is a valid cron expression

Since
5.3.8
nextback to summary
public <T extends Temporal & Comparable<? super T>> T next(T temporal)

Calculate the next Temporal that matches this expression.

Parameters
<T>
the type of temporal
temporal:T

the seed value

Returns:T

the next temporal that matches this expression, or null if no such temporal can be found

Annotations
@Nullable
nextOrSameback to summary
private <T extends Temporal & Comparable<? super T>> T nextOrSame(T temporal)
Annotations
@Nullable
nextOrSameInternalback to summary
private <T extends Temporal & Comparable<? super T>> T nextOrSameInternal(T temporal)
Annotations
@Nullable
parseback to summary
public static CronExpression parse(String expression)

Parse the given crontab expression string into a CronExpression. The string has six single space-separated time and date fields:

┌───────────── second (0-59)
│ ┌───────────── minute (0 - 59)
│ │ ┌───────────── hour (0 - 23)
│ │ │ ┌───────────── day of the month (1 - 31)
│ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
│ │ │ │ │ ┌───────────── day of the week (0 - 7)
│ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
│ │ │ │ │ │
* * * * * *

The following rules apply:

  • A field may be an asterisk (*), which always stands for "first-last". For the "day of the month" or "day of the week" fields, a question mark (?) may be used instead of an asterisk.
  • Ranges of numbers are expressed by two numbers separated with a hyphen (-). The specified range is inclusive.
  • Following a range (or *) with /n specifies the interval of the number's value through the range.
  • English names can also be used for the "month" and "day of week" fields. Use the first three letters of the particular day or month (case does not matter).
  • The "day of month" and "day of week" fields can contain a L-character, which stands for "last", and has a different meaning in each field:
    • In the "day of month" field, L stands for "the last day of the month". If followed by an negative offset (i.e. L-n), it means "nth-to-last day of the month". If followed by W (i.e. LW), it means "the last weekday of the month".
    • In the "day of week" field, dL or DDDL stands for "the last day of week d (or DDD) in the month".
  • The "day of month" field can be nW, which stands for "the nearest weekday to day of the month n". If n falls on Saturday, this yields the Friday before it. If n falls on Sunday, this yields the Monday after, which also happens if n is 1 and falls on a Saturday (i.e. 1W stands for "the first weekday of the month").
  • The "day of week" field can be d#n (or DDD#n), which stands for "the n-th day of week d (or DDD) in the month".

Example expressions:

  • "0 0 * * * *" = the top of every hour of every day.
  • "*/10 * * * * *" = every ten seconds.
  • "0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
  • "0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.
  • "0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
  • "0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
  • "0 0 0 25 12 ?" = every Christmas Day at midnight
  • "0 0 0 L * *" = last day of the month at midnight
  • "0 0 0 L-3 * *" = third-to-last day of the month at midnight
  • "0 0 0 1W * *" = first weekday of the month at midnight
  • "0 0 0 LW * *" = last weekday of the month at midnight
  • "0 0 0 * * 5L" = last Friday of the month at midnight
  • "0 0 0 * * THUL" = last Thursday of the month at midnight
  • "0 0 0 ? * 5#2" = the second Friday in the month at midnight
  • "0 0 0 ? * MON#1" = the first Monday in the month at midnight

The following macros are also supported:

  • "@yearly" (or "@annually") to run un once a year, i.e. "0 0 0 1 1 *",
  • "@monthly" to run once a month, i.e. "0 0 0 1 * *",
  • "@weekly" to run once a week, i.e. "0 0 0 * * 0",
  • "@daily" (or "@midnight") to run once a day, i.e. "0 0 0 * * *",
  • "@hourly" to run once an hour, i.e. "0 0 * * * *".
Parameters
expression:String

the expression string to parse

Returns:CronExpression

the parsed CronExpression object

Exceptions
IllegalArgumentException:
in the expression does not conform to the cron format
resolveMacrosback to summary
private static String resolveMacros(String expression)
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Return the expression string used to create this CronExpression.

Returns:String

Doc from java.lang.Object.toString.

a string representation of the object.

Annotations
@Override