public class CronExpressionEx extends CronExpression {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 5074132114460101284L;
public static final Integer ALL_SPEC = new Integer(ALL_SPEC_INT);
/**
* '?'
*/
public static final int NO_SPEC_INT = 98;
/**
* 秒
*/
private String secondsExp;
/**
* 分
*/
private String minutesExp;
/**
* 小时
*/
private String hoursExp;
/**
* 天
*/
private String daysOfMonthExp;
/**
* 月
*/
private String monthsExp;
/**
* 周
*/
private String daysOfWeekExp;
public CronExpressionEx(String cronExpression) throws ParseException {
super(cronExpression);
StringTokenizer exprsTok = new StringTokenizer(cronExpression, " \t", false);
secondsExp = exprsTok.nextToken().trim();
minutesExp = exprsTok.nextToken().trim();
hoursExp = exprsTok.nextToken().trim();
daysOfMonthExp = exprsTok.nextToken().trim();
monthsExp = exprsTok.nextToken().trim();
daysOfWeekExp = exprsTok.nextToken().trim();
}
public Set getSecondsSet() {
return seconds;
}
public String getSecondsField() {
return getExpressionSetSummary(seconds);
}
public Set getMinutesSet() {
return minutes;
}
public String getMinutesField() {
return getExpressionSetSummary(minutes);
}
public Set getHoursSet() {
return hours;
}
public String getHoursField() {
return getExpressionSetSummary(hours);
}
public Set getDaysOfMonthSet() {
return daysOfMonth;
}
public String getDaysOfMonthField() {
return getExpressionSetSummary(daysOfMonth);
}
public Set getMonthsSet() {
return months;
}
public String getMonthsField() {
return getExpressionSetSummary(months);
}
public Set getDaysOfWeekSet() {
return daysOfWeek;
}
public String getDaysOfWeekField() {
return getExpressionSetSummary(daysOfWeek);
}
public String getSecondsExp() {
return secondsExp;
}
public String getMinutesExp() {
return minutesExp;
}
public String getHoursExp() {
return hoursExp;
}
public String getDaysOfMonthExp() {
return daysOfMonthExp;
}
public String getMonthsExp() {
return monthsExp;
}
public String getDaysOfWeekExp() {
return daysOfWeekExp;
}
}
public static void main(String[] args) throws ParseException {
CronExpressionEx exp = new CronExpressionEx("0 0/10 * * * ?");
Date newDate = exp.getNextValidTimeAfter(new Date());
}
这样就可以获取到当前时间下的下一个执行时间,即10分钟后的时间
当然,也可以直接用原生类:
public static void main(String[] args) throws ParseException {
CronExpression exp = new CronExpression("0 0/10 * * * ?");
Date newDate = exp.getNextValidTimeAfter(new Date());
System.out.println(newDate);
}
效果是一样的