1.引入
有些時候我們不但需要定時執行任務,而且需要獲得下一次執行的時間。
但是我們執行時間配置的是cron表達式,不能夠根據上次執行的時間+執行間隔這種方式來獲得。所以我們必須要解析cron
2.方法
-
Date curTime = new Date();
-
System.out.println(curTime);
-
-
CronExpression expression;
-
try
-
{
-
expression = new CronExpression("0 30 15 * * ?");
-
Date newDate = expression.getNextValidTimeAfter(curTime);
-
System.out.println(newDate);
-
} catch (ParseException e) {
-
logger.error( "fail to parse cron express", e);
-
} catch (Exception e) {
-
logger.error( "fail to update rule nextTime", e);
-
}
結果為:
Wed Jun 24 19:11:52 CST 2015
Thu Jun 25 15:30:00 CST 2015
說明:
(1)當然需要引入Quartz的依賴
-
<dependency>
-
<groupId>org.opensymphony.quartz</groupId>
-
<artifactId>quartz-all</artifactId>
-
<version>1.6.1</version>
-
</dependency>
(2)getNextValidTimeAfter(Date date)是根據cron表達式,來獲得傳入時間之后的第一個執行時間
如上例中:當前時間為6月24日19:11:52,cron表示每天的15:30:00來執行,那么返回的結果就是6月25日15:30:00