public final class Schedule
extends java.lang.Object
This class provides methods that take a schedule and a time and return the next scheduled time. A cron-like string is used to define the schedule. The format is specified in the following BNF syntax:
The BNF syntax for specifying the second field is as follows:schedule := millisecond second minute hour dayOfMonth month dayOfWeek
Similar BNF statements for milliseconds, minute, hour, dayOfMonth, month, and dayOfWeek can be derived from the second syntax. The values for each field are defined as non-negative integers in the following ranges:second := * | secondList secondList := secondItem [, secondList] secondItem := secondValue | secondRange secondRange := secondValue - secondValue
Note: These values equate to the same ranges that the java.util.Calendar class uses, except for 'monthValue'. The java.util.Calendar range for 'monthValue' is 0-11, rather than 1-12.milliSecondValue := 0-999 secondValue := 0-59 minuteValue := 0-59 hourValue := 0-23 dayOfMonthValue := 1-31 monthValue := 1-12 dayOfWeekValue := 1-7
Using this syntax, each field can be represented as a range of values indicating all times between the two times. For example, "2-6" in the dayOfWeek field indicates Monday through Friday, inclusive. Each field can also be specified as a comma separated list. For instance, a minute field of "0,15,30,45" means every quarter hour on the quarter hour. Lastly, each field can be defined as both a set of individual values and ranges of values. For example, an hour field of "9-17,0" indicates between the hours of 9 am and 5 pm, and on the hour of midnight.
Additional semantics are as follows:
Examples:
"0 0 0,30 * * * *" Exact next nearest half-hour "* * 0,30 4-5 * * *" Anytime in the first minute of the half hours in the 4 and 5 o'clock AM hours "* * * 9-16 * * *" Between 9 AM and 5 PM (9:00.00 AM to 4:59.59PM) "* * * * 8-14 * 2" The second Tuesday of the month "* * * 13-16 * * 0" Between 1 PM and 5 pm on Sunday "* * * * * 31 *" The last day of the month "* * * * 15 4 1" The next time April 15th occurs on a Sunday "0 0 0 1 * * 2-6;0 0 0 2 * * 1,7" 1 AM on the weekdays, 2 AM on the weekends
Calendar
,
GregorianCalendar
,
TimeZone
Modifier and Type | Method | Description |
---|---|---|
static void |
main(java.lang.String[] args) |
Unsupported command line interface, may change without notice.
|
static java.util.Calendar |
nextScheduledTime(java.lang.String schedule) |
The next scheduled time after the current time.
|
static java.util.Calendar |
nextScheduledTime(java.lang.String schedule,
java.util.Calendar calendar) |
Get the next scheduled time after the given time.
|
static long |
nextScheduledTimeInMillis(java.lang.String schedule) |
The next scheduled time after the current time.
|
static long |
nextScheduledTimeInMillis(java.lang.String schedule,
long timeInMillis) |
The next scheduled time after the given
time.
|
static long |
nextScheduledTimeInMillisRelative(java.lang.String schedule) |
The next scheduled time after the current time.
|
static long |
nextScheduledTimeInMillisRelative(java.lang.String schedule,
long timeInMillis) |
Get the next scheduled time after the given time.
|
static void |
parseSchedule(java.lang.String schedule) |
Throw a ParseException if the given schedule doesn't parse.
|
public static void parseSchedule(java.lang.String schedule) throws java.text.ParseException
java.text.ParseException
- Syntax error in schedule.public static java.util.Calendar nextScheduledTime(java.lang.String schedule, java.util.Calendar calendar) throws java.text.ParseException
java.text.ParseException
- Syntax error in schedule.public static java.util.Calendar nextScheduledTime(java.lang.String schedule) throws java.text.ParseException
java.text.ParseException
nextScheduledTime(String, Calendar)
public static long nextScheduledTimeInMillis(java.lang.String schedule, long timeInMillis) throws java.text.ParseException
timeInMillis
- the time in UTC milliseconds from the epoch.java.text.ParseException
public static long nextScheduledTimeInMillis(java.lang.String schedule) throws java.text.ParseException
java.text.ParseException
public static long nextScheduledTimeInMillisRelative(java.lang.String schedule, long timeInMillis) throws java.text.ParseException
timeInMillis
- the time in UTC milliseconds from the epoch.java.text.ParseException
public static long nextScheduledTimeInMillisRelative(java.lang.String schedule) throws java.text.ParseException
java.text.ParseException
nextScheduledTimeInMillisRelative(String, long)
public static void main(java.lang.String[] args) throws java.lang.Exception
java.lang.Exception