统计Spring定时任务当天执行次数

以下代码为统计Spring定时任务当天执行的次数:

/**
 * 定时任务统计
 */
public class CronCountUtils {

	/**
	 * 计算一天内定时任务执行次数
	 * @param expression 表达式
	 * @return 执行次数
	 */
	public static final int count(String expression) {
		return count(null, expression);
	}
	
	/**
	 * 计算一天内定时任务执行次数
	 * @param date 时间
	 * @param expression 表达式
	 * @return 执行次数
	 */
	public static final int count(Date date, String expression) {
		CronSequenceGenerator generator = new CronSequenceGenerator(expression, TimeZone.getDefault());
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		if(date == null)
			date = calendar.getTime();
		int today = calendar.get(Calendar.DATE);
		int now = today;
		int count = 0;
		while(true) {
			date = generator.next(date);
			calendar.setTime(date);
			now = calendar.get(Calendar.DATE);
			if(now == today) {
				count++;
			} else {
				break;
			}
		}
		return count;
	}
	
	public static void main(String[] args) {
		System.out.println(count("0 0/40 06-07 * * ?"));
	}
	
}

判断是否是当天最后一次执行地址:http://www.acgist.com/article/354.html