cronexpression.java
来自「Quartz 是个开源的作业调度框架」· Java 代码 · 共 1,388 行 · 第 1/4 页
JAVA
1,388 行
buf.append("\n");
buf.append("hours: ");
buf.append(getExpressionSetSummary(hours));
buf.append("\n");
buf.append("daysOfMonth: ");
buf.append(getExpressionSetSummary(daysOfMonth));
buf.append("\n");
buf.append("months: ");
buf.append(getExpressionSetSummary(months));
buf.append("\n");
buf.append("daysOfWeek: ");
buf.append(getExpressionSetSummary(daysOfWeek));
buf.append("\n");
buf.append("lastdayOfWeek: ");
buf.append(lastdayOfWeek);
buf.append("\n");
buf.append("nearestWeekday: ");
buf.append(nearestWeekday);
buf.append("\n");
buf.append("NthDayOfWeek: ");
buf.append(nthdayOfWeek);
buf.append("\n");
buf.append("lastdayOfMonth: ");
buf.append(lastdayOfMonth);
buf.append("\n");
buf.append("calendardayOfWeek: ");
buf.append(calendardayOfWeek);
buf.append("\n");
buf.append("calendardayOfMonth: ");
buf.append(calendardayOfMonth);
buf.append("\n");
buf.append("years: ");
buf.append(getExpressionSetSummary(years));
buf.append("\n");
return buf.toString();
}
protected String getExpressionSetSummary(java.util.Set set) {
if (set.contains(NO_SPEC)) return "?";
if (set.contains(ALL_SPEC)) return "*";
StringBuffer buf = new StringBuffer();
Iterator itr = set.iterator();
boolean first = true;
while (itr.hasNext()) {
Integer iVal = (Integer) itr.next();
String val = iVal.toString();
if (!first) buf.append(",");
buf.append(val);
first = false;
}
return buf.toString();
}
protected String getExpressionSetSummary(java.util.ArrayList list) {
if (list.contains(NO_SPEC)) return "?";
if (list.contains(ALL_SPEC)) return "*";
StringBuffer buf = new StringBuffer();
Iterator itr = list.iterator();
boolean first = true;
while (itr.hasNext()) {
Integer iVal = (Integer) itr.next();
String val = iVal.toString();
if (!first) buf.append(",");
buf.append(val);
first = false;
}
return buf.toString();
}
protected int skipWhiteSpace(int i, String s) {
for (; i < s.length() && (s.charAt(i) == ' ' || s.charAt(i) == '\t'); i++)
;
return i;
}
protected int findNextWhiteSpace(int i, String s) {
for (; i < s.length() && (s.charAt(i) != ' ' || s.charAt(i) != '\t'); i++)
;
return i;
}
protected void addToSet(int val, int end, int incr, int type)
throws ParseException {
TreeSet set = getSet(type);
if (type == SECOND || type == MINUTE) {
if ((val < 0 || val > 59 || end > 59) && (val != ALL_SPEC_INT))
throw new ParseException(
"Minute and Second values must be between 0 and 59",
-1);
} else if (type == HOUR) {
if ((val < 0 || val > 23 || end > 23) && (val != ALL_SPEC_INT))
throw new ParseException(
"Hour values must be between 0 and 23", -1);
} else if (type == DAY_OF_MONTH) {
if ((val < 1 || val > 31 || end > 31) && (val != ALL_SPEC_INT)
&& (val != NO_SPEC_INT))
throw new ParseException(
"Day of month values must be between 1 and 31", -1);
} else if (type == MONTH) {
if ((val < 1 || val > 12 || end > 12) && (val != ALL_SPEC_INT))
throw new ParseException(
"Month values must be between 1 and 12", -1);
} else if (type == DAY_OF_WEEK) {
if ((val == 0 || val > 7 || end > 7) && (val != ALL_SPEC_INT)
&& (val != NO_SPEC_INT))
throw new ParseException(
"Day-of-Week values must be between 1 and 7", -1);
}
if ((incr == 0 || incr == -1) && val != ALL_SPEC_INT) {
if (val != -1) set.add(new Integer(val));
else
set.add(NO_SPEC);
return;
}
int startAt = val;
int stopAt = end;
if (val == ALL_SPEC_INT && incr <= 0) {
incr = 1;
set.add(ALL_SPEC); // put in a marker, but also fill values
}
if (type == SECOND || type == MINUTE) {
if (stopAt == -1) stopAt = 59;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 0;
} else if (type == HOUR) {
if (stopAt == -1) stopAt = 23;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 0;
} else if (type == DAY_OF_MONTH) {
if (stopAt == -1) stopAt = 31;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 1;
} else if (type == MONTH) {
if (stopAt == -1) stopAt = 12;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 1;
} else if (type == DAY_OF_WEEK) {
if (stopAt == -1) stopAt = 7;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 1;
} else if (type == YEAR) {
if (stopAt == -1) stopAt = 2099;
if (startAt == -1 || startAt == ALL_SPEC_INT) startAt = 1970;
}
for (int i = startAt; i <= stopAt; i += incr)
set.add(new Integer(i));
}
protected TreeSet getSet(int type) {
switch (type) {
case SECOND:
return seconds;
case MINUTE:
return minutes;
case HOUR:
return hours;
case DAY_OF_MONTH:
return daysOfMonth;
case MONTH:
return months;
case DAY_OF_WEEK:
return daysOfWeek;
case YEAR:
return years;
default:
return null;
}
}
protected ValueSet getValue(int v, String s, int i) {
char c = s.charAt(i);
String s1 = String.valueOf(v);
while (c >= '0' && c <= '9') {
s1 += c;
i++;
if (i >= s.length()) break;
c = s.charAt(i);
}
ValueSet val = new ValueSet();
if (i < s.length()) val.pos = i;
else
val.pos = i + 1;
val.value = Integer.parseInt(s1);
return val;
}
protected int getNumericValue(String s, int i) {
int endOfVal = findNextWhiteSpace(i, s);
String val = s.substring(i, endOfVal);
return Integer.parseInt(val);
}
protected int getMonthNumber(String s) {
Integer integer = (Integer) monthMap.get(s);
if (integer == null) return -1;
return integer.intValue();
}
protected int getDayOfWeekNumber(String s) {
Integer integer = (Integer) dayMap.get(s);
if (integer == null) return -1;
return integer.intValue();
}
protected Date getTime(int sc, int mn, int hr, int dayofmn, int mon) {
try {
Calendar cl = Calendar.getInstance(getTimeZone());
//cl.add(Calendar.DAY_OF_MONTH,);
if (hr >= 0 && hr <= 12) cl.set(Calendar.AM_PM, Calendar.AM);
if (hr >= 13 && hr <= 23) cl.set(Calendar.AM_PM, Calendar.PM);
cl.setLenient(false);
if (sc != -1) cl.set(Calendar.SECOND, sc);
if (mn != -1) cl.set(Calendar.MINUTE, mn);
if (hr != -1) cl.set(Calendar.HOUR_OF_DAY, hr);
if (dayofmn != -1) cl.set(Calendar.DAY_OF_MONTH, dayofmn);
if (mon != -1) cl.set(Calendar.MONTH, mon);
return cl.getTime();
} catch (Exception e) {
return null;
}
}
////////////////////////////////////////////////////////////////////////////
//
// Computation Functions
//
////////////////////////////////////////////////////////////////////////////
protected Date getTimeAfter(Date afterTime) {
Calendar cl = Calendar.getInstance(getTimeZone());
// move ahead one second, since we're computing the time *after* the
// given time
afterTime = new Date(afterTime.getTime() + 1000);
// CronTrigger does not deal with milliseconds
cl.setTime(afterTime);
cl.set(Calendar.MILLISECOND, 0);
boolean gotOne = false;
// loop until we've computed the next time, or we've past the endTime
while (!gotOne) {
//if (endTime != null && cl.getTime().after(endTime)) return null;
SortedSet st = null;
int t = 0;
int sec = cl.get(Calendar.SECOND);
int min = cl.get(Calendar.MINUTE);
// get second.................................................
st = seconds.tailSet(new Integer(sec));
if (st != null && st.size() != 0) {
sec = ((Integer) st.first()).intValue();
} else {
sec = ((Integer) seconds.first()).intValue();
min++;
cl.set(Calendar.MINUTE, min);
}
cl.set(Calendar.SECOND, sec);
min = cl.get(Calendar.MINUTE);
int hr = cl.get(Calendar.HOUR_OF_DAY);
t = -1;
// get minute.................................................
st = minutes.tailSet(new Integer(min));
if (st != null && st.size() != 0) {
t = min;
min = ((Integer) st.first()).intValue();
} else {
min = ((Integer) minutes.first()).intValue();
hr++;
}
if (min != t) {
cl.set(Calendar.SECOND, 0);
cl.set(Calendar.MINUTE, min);
setCalendarHour(cl, hr);
continue;
}
cl.set(Calendar.MINUTE, min);
hr = cl.get(Calendar.HOUR_OF_DAY);
int day = cl.get(Calendar.DAY_OF_MONTH);
t = -1;
// get hour...................................................
st = hours.tailSet(new Integer(hr));
if (st != null && st.size() != 0) {
t = hr;
hr = ((Integer) st.first()).intValue();
} else {
hr = ((Integer) hours.first()).intValue();
day++;
}
if (hr != t) {
cl.set(Calendar.SECOND, 0);
cl.set(Calendar.MINUTE, 0);
cl.set(Calendar.DAY_OF_MONTH, day);
setCalendarHour(cl, hr);
continue;
}
cl.set(Calendar.HOUR_OF_DAY, hr);
day = cl.get(Calendar.DAY_OF_MONTH);
int mon = cl.get(Calendar.MONTH) + 1;
// '+ 1' because calendar is 0-based for this field, and we are
// 1-based
t = -1;
int tmon = mon;
// get day...................................................
boolean dayOfMSpec = !daysOfMonth.contains(NO_SPEC);
boolean dayOfWSpec = !daysOfWeek.contains(NO_SPEC);
if (dayOfMSpec && !dayOfWSpec) { // get day by day of month rule
st = daysOfMonth.tailSet(new Integer(day));
if (lastdayOfMonth) {
if(!nearestWeekday) {
t = day;
day = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
}
else {
t = day;
day = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
java.util.Calendar tcal = java.util.Calendar.getInstance();
tcal.set(Calendar.SECOND, 0);
tcal.set(Calendar.MINUTE, 0);
tcal.set(Calendar.HOUR_OF_DAY, 0);
tcal.set(Calendar.DAY_OF_MONTH, day);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?