cron式に基づいて最近の実行時間を計算する
シーンを使用:
quartzをバックグラウンドタスクスケジューリングフレームワークとして使用し、cron式に時間を設定するには、cron式に基づいて最近のn回の実行の具体的な時間を計算する必要があります.これは、通常、ユーザーにタスクの実行時間を変更するためのヒントを開くときに役立ちます.
解決:
quartzのjarパッケージで提供されるTriggerUtilsクラスを使用して計算
具体的なコード:
quartzをバックグラウンドタスクスケジューリングフレームワークとして使用し、cron式に時間を設定するには、cron式に基づいて最近のn回の実行の具体的な時間を計算する必要があります.これは、通常、ユーザーにタスクの実行時間を変更するためのヒントを開くときに役立ちます.
解決:
quartzのjarパッケージで提供されるTriggerUtilsクラスを使用して計算
具体的なコード:
package com.crazycoder2010.quartz;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;
public class Main {
/**
* @param args
* @throws ParseException
* @throws InterruptedException
*/
public static void main(String[] args) throws ParseException, InterruptedException {
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression("0 0 15 5 * ?");// cron
Calendar calendar = Calendar.getInstance();
Date now = calendar.getTime();
calendar.add(Calendar.YEAR, 2);// 2 ( , 1 , 20 )
List<Date> dates = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, now, calendar.getTime());// , ~~
System.out.println(dates.size());
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for(int i =0;i < dates.size();i ++){
if(i >19){//
break;
}
System.out.println(dateFormat.format(dates.get(i)));
}
}
}