cron 表达式

网上的讲cron的博客都乱起八糟的,而且讲的不清楚,这篇博客把cron expression的每个点都清楚的表述了,有时间再翻译,原文地址:http://docwiki.embarcadero.com/Connect/en/Writing_a_CRON_Expression,还有一篇在spring中使用的文章http://www.mkyong.com/spring/spring-quartz-scheduler-example/


Writing a CRON Expression

Go Up to Overview of Scheduling

A CRON expression is a string of 6 or 7 fields, separated by a white space, that represents a schedule.

A CRON expression takes the following format (years are optional):

The string represents a set of times, which are the times that match the CRON expression. For example, 0 0 0 * * * is a daily schedule, because it matches combinations of date and time where seconds, minutes and hours are 0. If you change the hours field to 6, 0 0 6 * * *, your string represents every day at 6:00 AM. For more examples, see the list of examples below.

In each field you can use a number, a special character, or a combination of both. See Using Special Characters.

Contents

 [hide] 
  • 1 CRON Expression Format Details
  • 2 Using Special Characters
  • 3 CRON Expression Examples
  • 4 See Also

CRON Expression Format Details

Use the following rules to create a CRON expression:

Field Allowed Values Allowed Special Characters

Seconds

0-59

, - * /

Minutes

0-59

, - * /

Hours

0-23

, - * /

Day of month

1-31

, - * ? / L W

Month

1-12 or JAN-DEC

, - * /

Day of week

1-7 or SUN-SAT

, - * ? / L #

Year
(optional)

1970-2099

, - * /

Notes:
  • You must specify either day of month or day of week, but not both. Insert a question mark (?) as a placeholder for the one not specified.
  • If you do not specify the year, the year will be automatically determined by taking into consideration whether the date (month and day) inserted has already passed when compared to the current date of the system. If the date has not already passed, the current year is inserted. If the date has already passed, the next year is inserted.
  • The names of months and days of the week are not case sensitive. "MON" is the same as "mon".

Using Special Characters

The following table describes the legal special characters and how you can use them in a CRON expression:

Special Character Description

*
(all values)

Selects all values within a field.

For example, * in the minute field selects "every minute".

?
(no specific value)

Used to specify something in one of the two fields in which the character is allowed, but not the other.

For example, to make the trigger fire on a particular day of the month (say, the 10th), when it does not matter what day of the week that happens to be, put 10 in the day-of-month field, and ? in the day-of-week field.

-
(range)

Used to specify ranges.

For example, 10-12 in the hour field selects the hours 10, 11 and 12.

,
(comma)

Used to specify additional values.

For example, MON,WED,FRI in the day-of-week field means the days Monday, Wednesday, and Friday.

/
(forward slash)

Used to specify increments.

For example, 0/14 in the seconds field means the seconds 0, 14, 28, and 42; and 2/14 in the seconds field means the seconds 2, 16, 30, and 44.

L
(last)

Used differently in each of the two fields in which it is allowed:

  • In the day-of-month field, L selects the last day of the month, which is 31 for January and 29 for February on leap years.
  • When used in the day-of-week field by itself, it means Saturday. But if used in the day-of-week field after another value, L selects the last xx day of the month. For example, 6L selects the last Friday of the month.

When using the L special character, do not specify lists, or ranges of values, because this may give confusing results.

W
(weekday)

Used to specify the weekday (Monday-Friday) nearest to the given day.

For example, if you specify 15W as the value for the day-of-month field, the nearest weekday to the 15th of the month is selected. So if the 15th is a Saturday, Friday the 14th is selected. If the 15th is a Sunday, Monday the 16th is selected. If the 15th is a Tuesday, Tuesday the 15th is selected.

However if you specify 1W as the value for day-of-month, and the 1st is a Saturday, Monday the 3rd is selected, as the selection rules do not allow for crossing over the boundary of a month's days to the previous or the subsequent month.

The W character can only be used to specify a single day, not a range or list of days.

#

Used to specify the nth XXX (or XX) day of the month.

For example, the value FRI#3 or 6#3 in the day-of-week field means the third Friday of the month (6 or FRI = Friday, and #3 = the 3rd one in the month).

Note: The  L and  W characters can also be combined in the day-of-month field to yield  LW, which translates to "last weekday of the month".

CRON Expression Examples

The following table lists some examples of CRON expressions:

CRON Expression Meaning

0 0 12 * * ?

12 PM (noon) every day.

0 15 10 ? * *

10:15 AM every day.

0 15 10 * * ?

10:15 AM every day.

0 15 10 * * ? *

10:15 AM every day.

0 15 10 * * ? 2005

10:15 AM every day during the year 2005.

0 * 14 * * ?

Every minute starting at 2 PM and ending at 2:59 PM, every day.

0 0/5 14 * * ?

Every 5 minutes starting at 2 PM and ending at 2:55 PM, every day.

0 0/5 14,18 * * ?

Every 5 minutes starting at 2 PM and ending at 2:55 PM, and every 5 minutes starting at 6 PM and ending at 6:55 PM, every day.

0 0-5 14 * * ?

Every minute starting at 2 PM and ending at 2:05 PM, every day.

0 10,44 14 ? 3 WED

2:10 PM and at 2:44 PM every Wednesday in the month of March.

0 15 10 ? * MON-FRI

10:15 AM every Monday, Tuesday, Wednesday, Thursday and Friday.

0 15 10 15 * ?

10:15 AM on the 15th day of every month.

0 15 10 L * ?

10:15 AM on the last day of every month.

0 15 10 ? * 6L

10:15 AM on the last Friday of every month.

0 15 10 ? * 6L 2002-2005

10:15 AM on every last Friday of every month during the years 2002, 2003, 2004 and 2005.

0 15 10 ? * 6#3

10:15 AM on the third Friday of every month.

0 0 12 1/5 * ?

12 PM (noon) every 5 days every month, starting on the first day of the month.

0 11 11 11 11 ?

Every November 11th at 11:11 AM.


你可能感兴趣的:(其他,cronexpression,定时器,quartz)