PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/drools-core/src/main/java/org/drools/time/impl/CronExpression.java

https://github.com/mdproctor/drools
Java | 1594 lines | 1112 code | 165 blank | 317 comment | 477 complexity | 229289b4a0eb981b78fff30a7bd6f83f MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * Copyright 2010 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.drools.time.impl;
  17. import java.io.Serializable;
  18. import java.text.ParseException;
  19. import java.util.Calendar;
  20. import java.util.Date;
  21. import java.util.HashMap;
  22. import java.util.Iterator;
  23. import java.util.Locale;
  24. import java.util.Map;
  25. import java.util.SortedSet;
  26. import java.util.StringTokenizer;
  27. import java.util.TimeZone;
  28. import java.util.TreeSet;
  29. /**
  30. * Provides a parser and evaluator for unix-like cron expressions. Cron
  31. * expressions provide the ability to specify complex time combinations such as
  32. * "At 8:00am every Monday through Friday" or "At 1:30am every
  33. * last Friday of the month".
  34. * <P>
  35. * Cron expressions are comprised of 6 required fields and one optional field
  36. * separated by white space. The fields respectively are described as follows:
  37. *
  38. * <table cellspacing="8">
  39. * <tr>
  40. * <th align="left">Field Name</th>
  41. * <th align="left">&nbsp;</th>
  42. * <th align="left">Allowed Values</th>
  43. * <th align="left">&nbsp;</th>
  44. * <th align="left">Allowed Special Characters</th>
  45. * </tr>
  46. * <tr>
  47. * <td align="left"><code>Seconds</code></td>
  48. * <td align="left">&nbsp;</th>
  49. * <td align="left"><code>0-59</code></td>
  50. * <td align="left">&nbsp;</th>
  51. * <td align="left"><code>, - * /</code></td>
  52. * </tr>
  53. * <tr>
  54. * <td align="left"><code>Minutes</code></td>
  55. * <td align="left">&nbsp;</th>
  56. * <td align="left"><code>0-59</code></td>
  57. * <td align="left">&nbsp;</th>
  58. * <td align="left"><code>, - * /</code></td>
  59. * </tr>
  60. * <tr>
  61. * <td align="left"><code>Hours</code></td>
  62. * <td align="left">&nbsp;</th>
  63. * <td align="left"><code>0-23</code></td>
  64. * <td align="left">&nbsp;</th>
  65. * <td align="left"><code>, - * /</code></td>
  66. * </tr>
  67. * <tr>
  68. * <td align="left"><code>Day-of-month</code></td>
  69. * <td align="left">&nbsp;</th>
  70. * <td align="left"><code>1-31</code></td>
  71. * <td align="left">&nbsp;</th>
  72. * <td align="left"><code>, - * ? / L W</code></td>
  73. * </tr>
  74. * <tr>
  75. * <td align="left"><code>Month</code></td>
  76. * <td align="left">&nbsp;</th>
  77. * <td align="left"><code>1-12 or JAN-DEC</code></td>
  78. * <td align="left">&nbsp;</th>
  79. * <td align="left"><code>, - * /</code></td>
  80. * </tr>
  81. * <tr>
  82. * <td align="left"><code>Day-of-Week</code></td>
  83. * <td align="left">&nbsp;</th>
  84. * <td align="left"><code>1-7 or SUN-SAT</code></td>
  85. * <td align="left">&nbsp;</th>
  86. * <td align="left"><code>, - * ? / L #</code></td>
  87. * </tr>
  88. * <tr>
  89. * <td align="left"><code>Year (Optional)</code></td>
  90. * <td align="left">&nbsp;</th>
  91. * <td align="left"><code>empty, 1970-2199</code></td>
  92. * <td align="left">&nbsp;</th>
  93. * <td align="left"><code>, - * /</code></td>
  94. * </tr>
  95. * </table>
  96. * <P>
  97. * The '*' character is used to specify all values. For example, &quot;*&quot;
  98. * in the minute field means &quot;every minute&quot;.
  99. * <P>
  100. * The '?' character is allowed for the day-of-month and day-of-week fields. It
  101. * is used to specify 'no specific value'. This is useful when you need to
  102. * specify something in one of the two fields, but not the other.
  103. * <P>
  104. * The '-' character is used to specify ranges For example &quot;10-12&quot; in
  105. * the hour field means &quot;the hours 10, 11 and 12&quot;.
  106. * <P>
  107. * The ',' character is used to specify additional values. For example
  108. * &quot;MON,WED,FRI&quot; in the day-of-week field means &quot;the days Monday,
  109. * Wednesday, and Friday&quot;.
  110. * <P>
  111. * The '/' character is used to specify increments. For example &quot;0/15&quot;
  112. * in the seconds field means &quot;the seconds 0, 15, 30, and 45&quot;. And
  113. * &quot;5/15&quot; in the seconds field means &quot;the seconds 5, 20, 35, and
  114. * 50&quot;. Specifying '*' before the '/' is equivalent to specifying 0 is
  115. * the value to start with. Essentially, for each field in the expression, there
  116. * is a set of numbers that can be turned on or off. For seconds and minutes,
  117. * the numbers range from 0 to 59. For hours 0 to 23, for days of the month 0 to
  118. * 31, and for months 1 to 12. The &quot;/&quot; character simply helps you turn
  119. * on every &quot;nth&quot; value in the given set. Thus &quot;7/6&quot; in the
  120. * month field only turns on month &quot;7&quot;, it does NOT mean every 6th
  121. * month, please note that subtlety.
  122. * <P>
  123. * The 'L' character is allowed for the day-of-month and day-of-week fields.
  124. * This character is short-hand for &quot;last&quot;, but it has different
  125. * meaning in each of the two fields. For example, the value &quot;L&quot; in
  126. * the day-of-month field means &quot;the last day of the month&quot; - day 31
  127. * for January, day 28 for February on non-leap years. If used in the
  128. * day-of-week field by itself, it simply means &quot;7&quot; or
  129. * &quot;SAT&quot;. But if used in the day-of-week field after another value, it
  130. * means &quot;the last xxx day of the month&quot; - for example &quot;6L&quot;
  131. * means &quot;the last friday of the month&quot;. When using the 'L' option, it
  132. * is important not to specify lists, or ranges of values, as you'll get
  133. * confusing results.
  134. * <P>
  135. * The 'W' character is allowed for the day-of-month field. This character
  136. * is used to specify the weekday (Monday-Friday) nearest the given day. As an
  137. * example, if you were to specify &quot;15W&quot; as the value for the
  138. * day-of-month field, the meaning is: &quot;the nearest weekday to the 15th of
  139. * the month&quot;. So if the 15th is a Saturday, the trigger will fire on
  140. * Friday the 14th. If the 15th is a Sunday, the trigger will fire on Monday the
  141. * 16th. If the 15th is a Tuesday, then it will fire on Tuesday the 15th.
  142. * However if you specify &quot;1W&quot; as the value for day-of-month, and the
  143. * 1st is a Saturday, the trigger will fire on Monday the 3rd, as it will not
  144. * 'jump' over the boundary of a month's days. The 'W' character can only be
  145. * specified when the day-of-month is a single day, not a range or list of days.
  146. * <P>
  147. * The 'L' and 'W' characters can also be combined for the day-of-month
  148. * expression to yield 'LW', which translates to &quot;last weekday of the
  149. * month&quot;.
  150. * <P>
  151. * The '#' character is allowed for the day-of-week field. This character is
  152. * used to specify &quot;the nth&quot; XXX day of the month. For example, the
  153. * value of &quot;6#3&quot; in the day-of-week field means the third Friday of
  154. * the month (day 6 = Friday and &quot;#3&quot; = the 3rd one in the month).
  155. * Other examples: &quot;2#1&quot; = the first Monday of the month and
  156. * &quot;4#5&quot; = the fifth Wednesday of the month. Note that if you specify
  157. * &quot;#5&quot; and there is not 5 of the given day-of-week in the month, then
  158. * no firing will occur that month. If the '#' character is used, there can
  159. * only be one expression in the day-of-week field (&quot;3#1,6#3&quot; is
  160. * not valid, since there are two expressions).
  161. * <P>
  162. * <!--The 'C' character is allowed for the day-of-month and day-of-week fields.
  163. * This character is short-hand for "calendar". This means values are
  164. * calculated against the associated calendar, if any. If no calendar is
  165. * associated, then it is equivalent to having an all-inclusive calendar. A
  166. * value of "5C" in the day-of-month field means "the first day included by the
  167. * calendar on or after the 5th". A value of "1C" in the day-of-week field
  168. * means "the first day included by the calendar on or after sunday".-->
  169. * <P>
  170. * The legal characters and the names of months and days of the week are not
  171. * case sensitive.
  172. *
  173. * <p>
  174. * <b>NOTES:</b>
  175. * <ul>
  176. * <li>Support for specifying both a day-of-week and a day-of-month value is
  177. * not complete (you'll need to use the '?' character in one of these fields).
  178. * </li>
  179. * <li>Overflowing ranges is supported - that is, having a larger number on
  180. * the left hand side than the right. You might do 22-2 to catch 10 o'clock
  181. * at night until 2 o'clock in the morning, or you might have NOV-FEB. It is
  182. * very important to note that overuse of overflowing ranges creates ranges
  183. * that don't make sense and no effort has been made to determine which
  184. * interpretation CronExpression chooses. An example would be
  185. * "0 0 14-6 ? * FRI-MON". </li>
  186. * </ul>
  187. * </p>
  188. */
  189. public class CronExpression implements Serializable, Cloneable {
  190. private static final long serialVersionUID = 510l;
  191. protected static final int SECOND = 0;
  192. protected static final int MINUTE = 1;
  193. protected static final int HOUR = 2;
  194. protected static final int DAY_OF_MONTH = 3;
  195. protected static final int MONTH = 4;
  196. protected static final int DAY_OF_WEEK = 5;
  197. protected static final int YEAR = 6;
  198. protected static final int ALL_SPEC_INT = 99; // '*'
  199. protected static final int NO_SPEC_INT = 98; // '?'
  200. protected static final Integer ALL_SPEC = new Integer(ALL_SPEC_INT);
  201. protected static final Integer NO_SPEC = new Integer(NO_SPEC_INT);
  202. protected static final Map monthMap = new HashMap(20);
  203. protected static final Map dayMap = new HashMap(60);
  204. static {
  205. monthMap.put("JAN", new Integer(0));
  206. monthMap.put("FEB", new Integer(1));
  207. monthMap.put("MAR", new Integer(2));
  208. monthMap.put("APR", new Integer(3));
  209. monthMap.put("MAY", new Integer(4));
  210. monthMap.put("JUN", new Integer(5));
  211. monthMap.put("JUL", new Integer(6));
  212. monthMap.put("AUG", new Integer(7));
  213. monthMap.put("SEP", new Integer(8));
  214. monthMap.put("OCT", new Integer(9));
  215. monthMap.put("NOV", new Integer(10));
  216. monthMap.put("DEC", new Integer(11));
  217. dayMap.put("SUN", new Integer(1));
  218. dayMap.put("MON", new Integer(2));
  219. dayMap.put("TUE", new Integer(3));
  220. dayMap.put("WED", new Integer(4));
  221. dayMap.put("THU", new Integer(5));
  222. dayMap.put("FRI", new Integer(6));
  223. dayMap.put("SAT", new Integer(7));
  224. }
  225. private String cronExpression = null;
  226. private TimeZone timeZone = null;
  227. protected transient TreeSet seconds;
  228. protected transient TreeSet minutes;
  229. protected transient TreeSet hours;
  230. protected transient TreeSet daysOfMonth;
  231. protected transient TreeSet months;
  232. protected transient TreeSet daysOfWeek;
  233. protected transient TreeSet years;
  234. protected transient boolean lastdayOfWeek = false;
  235. protected transient int nthdayOfWeek = 0;
  236. protected transient boolean lastdayOfMonth = false;
  237. protected transient boolean nearestWeekday = false;
  238. protected transient boolean expressionParsed = false;
  239. /**
  240. * Constructs a new <CODE>CronExpression</CODE> based on the specified
  241. * parameter.
  242. *
  243. * @param cronExpression String representation of the cron expression the
  244. * new object should represent
  245. * @throws java.text.ParseException
  246. * if the string expression cannot be parsed into a valid
  247. * <CODE>CronExpression</CODE>
  248. */
  249. public CronExpression(String cronExpression) throws ParseException {
  250. if (cronExpression == null) {
  251. throw new IllegalArgumentException("cronExpression cannot be null");
  252. }
  253. this.cronExpression = cronExpression.toUpperCase(Locale.US);
  254. buildExpression(this.cronExpression);
  255. }
  256. /**
  257. * Indicates whether the given date satisfies the cron expression. Note that
  258. * milliseconds are ignored, so two Dates falling on different milliseconds
  259. * of the same second will always have the same result here.
  260. *
  261. * @param date the date to evaluate
  262. * @return a boolean indicating whether the given date satisfies the cron
  263. * expression
  264. */
  265. public boolean isSatisfiedBy(Date date) {
  266. Calendar testDateCal = Calendar.getInstance(getTimeZone());
  267. testDateCal.setTime(date);
  268. testDateCal.set(Calendar.MILLISECOND, 0);
  269. Date originalDate = testDateCal.getTime();
  270. testDateCal.add(Calendar.SECOND, -1);
  271. Date timeAfter = getTimeAfter(testDateCal.getTime());
  272. return ((timeAfter != null) && (timeAfter.equals(originalDate)));
  273. }
  274. /**
  275. * Returns the next date/time <I>after</I> the given date/time which
  276. * satisfies the cron expression.
  277. *
  278. * @param date the date/time at which to begin the search for the next valid
  279. * date/time
  280. * @return the next valid date/time
  281. */
  282. public Date getNextValidTimeAfter(Date date) {
  283. return getTimeAfter(date);
  284. }
  285. /**
  286. * Returns the next date/time <I>after</I> the given date/time which does
  287. * <I>not</I> satisfy the expression
  288. *
  289. * @param date the date/time at which to begin the search for the next
  290. * invalid date/time
  291. * @return the next valid date/time
  292. */
  293. public Date getNextInvalidTimeAfter(Date date) {
  294. long difference = 1000;
  295. //move back to the nearest second so differences will be accurate
  296. Calendar adjustCal = Calendar.getInstance(getTimeZone());
  297. adjustCal.setTime(date);
  298. adjustCal.set(Calendar.MILLISECOND, 0);
  299. Date lastDate = adjustCal.getTime();
  300. Date newDate = null;
  301. //TODO: (QUARTZ-481) IMPROVE THIS! The following is a BAD solution to this problem. Performance will be very bad here, depending on the cron expression. It is, however A solution.
  302. //keep getting the next included time until it's farther than one second
  303. // apart. At that point, lastDate is the last valid fire time. We return
  304. // the second immediately following it.
  305. while (difference == 1000) {
  306. newDate = getTimeAfter(lastDate);
  307. difference = newDate.getTime() - lastDate.getTime();
  308. if (difference == 1000) {
  309. lastDate = newDate;
  310. }
  311. }
  312. return new Date(lastDate.getTime() + 1000);
  313. }
  314. /**
  315. * Returns the time zone for which this <code>CronExpression</code>
  316. * will be resolved.
  317. */
  318. public TimeZone getTimeZone() {
  319. if (timeZone == null) {
  320. timeZone = TimeZone.getDefault();
  321. }
  322. return timeZone;
  323. }
  324. /**
  325. * Sets the time zone for which this <code>CronExpression</code>
  326. * will be resolved.
  327. */
  328. public void setTimeZone(TimeZone timeZone) {
  329. this.timeZone = timeZone;
  330. }
  331. /**
  332. * Returns the string representation of the <CODE>CronExpression</CODE>
  333. *
  334. * @return a string representation of the <CODE>CronExpression</CODE>
  335. */
  336. public String toString() {
  337. return cronExpression;
  338. }
  339. /**
  340. * Indicates whether the specified cron expression can be parsed into a
  341. * valid cron expression
  342. *
  343. * @param cronExpression the expression to evaluate
  344. * @return a boolean indicating whether the given expression is a valid cron
  345. * expression
  346. */
  347. public static boolean isValidExpression(String cronExpression) {
  348. try {
  349. new CronExpression(cronExpression);
  350. } catch (ParseException pe) {
  351. return false;
  352. }
  353. return true;
  354. }
  355. ////////////////////////////////////////////////////////////////////////////
  356. //
  357. // Expression Parsing Functions
  358. //
  359. ////////////////////////////////////////////////////////////////////////////
  360. protected void buildExpression(String expression) throws ParseException {
  361. expressionParsed = true;
  362. try {
  363. if (seconds == null) {
  364. seconds = new TreeSet();
  365. }
  366. if (minutes == null) {
  367. minutes = new TreeSet();
  368. }
  369. if (hours == null) {
  370. hours = new TreeSet();
  371. }
  372. if (daysOfMonth == null) {
  373. daysOfMonth = new TreeSet();
  374. }
  375. if (months == null) {
  376. months = new TreeSet();
  377. }
  378. if (daysOfWeek == null) {
  379. daysOfWeek = new TreeSet();
  380. }
  381. if (years == null) {
  382. years = new TreeSet();
  383. }
  384. int exprOn = SECOND;
  385. StringTokenizer exprsTok = new StringTokenizer(expression, " \t",
  386. false);
  387. while (exprsTok.hasMoreTokens() && exprOn <= YEAR) {
  388. String expr = exprsTok.nextToken().trim();
  389. // throw an exception if L is used with other days of the month
  390. if(exprOn == DAY_OF_MONTH && expr.indexOf('L') != -1 && expr.length() > 1 && expr.indexOf(",") >= 0) {
  391. throw new ParseException("Support for specifying 'L' and 'LW' with other days of the month is not implemented", -1);
  392. }
  393. // throw an exception if L is used with other days of the week
  394. if(exprOn == DAY_OF_WEEK && expr.indexOf('L') != -1 && expr.length() > 1 && expr.indexOf(",") >= 0) {
  395. throw new ParseException("Support for specifying 'L' with other days of the week is not implemented", -1);
  396. }
  397. StringTokenizer vTok = new StringTokenizer(expr, ",");
  398. while (vTok.hasMoreTokens()) {
  399. String v = vTok.nextToken();
  400. storeExpressionVals(0, v, exprOn);
  401. }
  402. exprOn++;
  403. }
  404. if (exprOn <= DAY_OF_WEEK) {
  405. throw new ParseException("Unexpected end of expression.",
  406. expression.length());
  407. }
  408. if (exprOn <= YEAR) {
  409. storeExpressionVals(0, "*", YEAR);
  410. }
  411. TreeSet dow = getSet(DAY_OF_WEEK);
  412. TreeSet dom = getSet(DAY_OF_MONTH);
  413. // Copying the logic from the UnsupportedOperationException below
  414. boolean dayOfMSpec = !dom.contains(NO_SPEC);
  415. boolean dayOfWSpec = !dow.contains(NO_SPEC);
  416. if (dayOfMSpec && !dayOfWSpec) {
  417. // skip
  418. } else if (dayOfWSpec && !dayOfMSpec) {
  419. // skip
  420. } else {
  421. throw new ParseException(
  422. "Support for specifying both a day-of-week AND a day-of-month parameter is not implemented.", 0);
  423. }
  424. } catch (ParseException pe) {
  425. throw pe;
  426. } catch (Exception e) {
  427. throw new ParseException("Illegal cron expression format ("
  428. + e.toString() + ")", 0);
  429. }
  430. }
  431. protected int storeExpressionVals(int pos, String s, int type)
  432. throws ParseException {
  433. int incr = 0;
  434. int i = skipWhiteSpace(pos, s);
  435. if (i >= s.length()) {
  436. return i;
  437. }
  438. char c = s.charAt(i);
  439. if ((c >= 'A') && (c <= 'Z') && (!s.equals("L")) && (!s.equals("LW"))) {
  440. String sub = s.substring(i, i + 3);
  441. int sval = -1;
  442. int eval = -1;
  443. if (type == MONTH) {
  444. sval = getMonthNumber(sub) + 1;
  445. if (sval <= 0) {
  446. throw new ParseException("Invalid Month value: '" + sub + "'", i);
  447. }
  448. if (s.length() > i + 3) {
  449. c = s.charAt(i + 3);
  450. if (c == '-') {
  451. i += 4;
  452. sub = s.substring(i, i + 3);
  453. eval = getMonthNumber(sub) + 1;
  454. if (eval <= 0) {
  455. throw new ParseException("Invalid Month value: '" + sub + "'", i);
  456. }
  457. }
  458. }
  459. } else if (type == DAY_OF_WEEK) {
  460. sval = getDayOfWeekNumber(sub);
  461. if (sval < 0) {
  462. throw new ParseException("Invalid Day-of-Week value: '"
  463. + sub + "'", i);
  464. }
  465. if (s.length() > i + 3) {
  466. c = s.charAt(i + 3);
  467. if (c == '-') {
  468. i += 4;
  469. sub = s.substring(i, i + 3);
  470. eval = getDayOfWeekNumber(sub);
  471. if (eval < 0) {
  472. throw new ParseException(
  473. "Invalid Day-of-Week value: '" + sub
  474. + "'", i);
  475. }
  476. } else if (c == '#') {
  477. try {
  478. i += 4;
  479. nthdayOfWeek = Integer.parseInt(s.substring(i));
  480. if (nthdayOfWeek < 1 || nthdayOfWeek > 5) {
  481. throw new Exception();
  482. }
  483. } catch (Exception e) {
  484. throw new ParseException(
  485. "A numeric value between 1 and 5 must follow the '#' option",
  486. i);
  487. }
  488. } else if (c == 'L') {
  489. lastdayOfWeek = true;
  490. i++;
  491. }
  492. }
  493. } else {
  494. throw new ParseException(
  495. "Illegal characters for this position: '" + sub + "'",
  496. i);
  497. }
  498. if (eval != -1) {
  499. incr = 1;
  500. }
  501. addToSet(sval, eval, incr, type);
  502. return (i + 3);
  503. }
  504. if (c == '?') {
  505. i++;
  506. if ((i + 1) < s.length()
  507. && (s.charAt(i) != ' ' && s.charAt(i + 1) != '\t')) {
  508. throw new ParseException("Illegal character after '?': "
  509. + s.charAt(i), i);
  510. }
  511. if (type != DAY_OF_WEEK && type != DAY_OF_MONTH) {
  512. throw new ParseException(
  513. "'?' can only be specfied for Day-of-Month or Day-of-Week.",
  514. i);
  515. }
  516. if (type == DAY_OF_WEEK && !lastdayOfMonth) {
  517. int val = ((Integer) daysOfMonth.last()).intValue();
  518. if (val == NO_SPEC_INT) {
  519. throw new ParseException(
  520. "'?' can only be specfied for Day-of-Month -OR- Day-of-Week.",
  521. i);
  522. }
  523. }
  524. addToSet(NO_SPEC_INT, -1, 0, type);
  525. return i;
  526. }
  527. if (c == '*' || c == '/') {
  528. if (c == '*' && (i + 1) >= s.length()) {
  529. addToSet(ALL_SPEC_INT, -1, incr, type);
  530. return i + 1;
  531. } else if (c == '/'
  532. && ((i + 1) >= s.length() || s.charAt(i + 1) == ' ' || s
  533. .charAt(i + 1) == '\t')) {
  534. throw new ParseException("'/' must be followed by an integer.", i);
  535. } else if (c == '*') {
  536. i++;
  537. }
  538. c = s.charAt(i);
  539. if (c == '/') { // is an increment specified?
  540. i++;
  541. if (i >= s.length()) {
  542. throw new ParseException("Unexpected end of string.", i);
  543. }
  544. incr = getNumericValue(s, i);
  545. i++;
  546. if (incr > 10) {
  547. i++;
  548. }
  549. if (incr > 59 && (type == SECOND || type == MINUTE)) {
  550. throw new ParseException("Increment > 60 : " + incr, i);
  551. } else if (incr > 23 && (type == HOUR)) {
  552. throw new ParseException("Increment > 24 : " + incr, i);
  553. } else if (incr > 31 && (type == DAY_OF_MONTH)) {
  554. throw new ParseException("Increment > 31 : " + incr, i);
  555. } else if (incr > 7 && (type == DAY_OF_WEEK)) {
  556. throw new ParseException("Increment > 7 : " + incr, i);
  557. } else if (incr > 12 && (type == MONTH)) {
  558. throw new ParseException("Increment > 12 : " + incr, i);
  559. }
  560. } else {
  561. incr = 1;
  562. }
  563. addToSet(ALL_SPEC_INT, -1, incr, type);
  564. return i;
  565. } else if (c == 'L') {
  566. i++;
  567. if (type == DAY_OF_MONTH) {
  568. lastdayOfMonth = true;
  569. }
  570. if (type == DAY_OF_WEEK) {
  571. addToSet(7, 7, 0, type);
  572. }
  573. if(type == DAY_OF_MONTH && s.length() > i) {
  574. c = s.charAt(i);
  575. if(c == 'W') {
  576. nearestWeekday = true;
  577. i++;
  578. }
  579. }
  580. return i;
  581. } else if (c >= '0' && c <= '9') {
  582. int val = Integer.parseInt(String.valueOf(c));
  583. i++;
  584. if (i >= s.length()) {
  585. addToSet(val, -1, -1, type);
  586. } else {
  587. c = s.charAt(i);
  588. if (c >= '0' && c <= '9') {
  589. ValueSet vs = getValue(val, s, i);
  590. val = vs.value;
  591. i = vs.pos;
  592. }
  593. i = checkNext(i, s, val, type);
  594. return i;
  595. }
  596. } else {
  597. throw new ParseException("Unexpected character: " + c, i);
  598. }
  599. return i;
  600. }
  601. protected int checkNext(int pos, String s, int val, int type)
  602. throws ParseException {
  603. int end = -1;
  604. int i = pos;
  605. if (i >= s.length()) {
  606. addToSet(val, end, -1, type);
  607. return i;
  608. }
  609. char c = s.charAt(pos);
  610. if (c == 'L') {
  611. if (type == DAY_OF_WEEK) {
  612. lastdayOfWeek = true;
  613. } else {
  614. throw new ParseException("'L' option is not valid here. (pos=" + i + ")", i);
  615. }
  616. TreeSet set = getSet(type);
  617. set.add(new Integer(val));
  618. i++;
  619. return i;
  620. }
  621. if (c == 'W') {
  622. if (type == DAY_OF_MONTH) {
  623. nearestWeekday = true;
  624. } else {
  625. throw new ParseException("'W' option is not valid here. (pos=" + i + ")", i);
  626. }
  627. TreeSet set = getSet(type);
  628. set.add(new Integer(val));
  629. i++;
  630. return i;
  631. }
  632. if (c == '#') {
  633. if (type != DAY_OF_WEEK) {
  634. throw new ParseException("'#' option is not valid here. (pos=" + i + ")", i);
  635. }
  636. i++;
  637. try {
  638. nthdayOfWeek = Integer.parseInt(s.substring(i));
  639. if (nthdayOfWeek < 1 || nthdayOfWeek > 5) {
  640. throw new Exception();
  641. }
  642. } catch (Exception e) {
  643. throw new ParseException(
  644. "A numeric value between 1 and 5 must follow the '#' option",
  645. i);
  646. }
  647. TreeSet set = getSet(type);
  648. set.add(new Integer(val));
  649. i++;
  650. return i;
  651. }
  652. if (c == '-') {
  653. i++;
  654. c = s.charAt(i);
  655. int v = Integer.parseInt(String.valueOf(c));
  656. end = v;
  657. i++;
  658. if (i >= s.length()) {
  659. addToSet(val, end, 1, type);
  660. return i;
  661. }
  662. c = s.charAt(i);
  663. if (c >= '0' && c <= '9') {
  664. ValueSet vs = getValue(v, s, i);
  665. int v1 = vs.value;
  666. end = v1;
  667. i = vs.pos;
  668. }
  669. if (i < s.length() && ((c = s.charAt(i)) == '/')) {
  670. i++;
  671. c = s.charAt(i);
  672. int v2 = Integer.parseInt(String.valueOf(c));
  673. i++;
  674. if (i >= s.length()) {
  675. addToSet(val, end, v2, type);
  676. return i;
  677. }
  678. c = s.charAt(i);
  679. if (c >= '0' && c <= '9') {
  680. ValueSet vs = getValue(v2, s, i);
  681. int v3 = vs.value;
  682. addToSet(val, end, v3, type);
  683. i = vs.pos;
  684. return i;
  685. } else {
  686. addToSet(val, end, v2, type);
  687. return i;
  688. }
  689. } else {
  690. addToSet(val, end, 1, type);
  691. return i;
  692. }
  693. }
  694. if (c == '/') {
  695. i++;
  696. c = s.charAt(i);
  697. int v2 = Integer.parseInt(String.valueOf(c));
  698. i++;
  699. if (i >= s.length()) {
  700. addToSet(val, end, v2, type);
  701. return i;
  702. }
  703. c = s.charAt(i);
  704. if (c >= '0' && c <= '9') {
  705. ValueSet vs = getValue(v2, s, i);
  706. int v3 = vs.value;
  707. addToSet(val, end, v3, type);
  708. i = vs.pos;
  709. return i;
  710. } else {
  711. throw new ParseException("Unexpected character '" + c + "' after '/'", i);
  712. }
  713. }
  714. addToSet(val, end, 0, type);
  715. i++;
  716. return i;
  717. }
  718. public String getCronExpression() {
  719. return cronExpression;
  720. }
  721. public String getExpressionSummary() {
  722. StringBuffer buf = new StringBuffer();
  723. buf.append("seconds: ");
  724. buf.append(getExpressionSetSummary(seconds));
  725. buf.append("\n");
  726. buf.append("minutes: ");
  727. buf.append(getExpressionSetSummary(minutes));
  728. buf.append("\n");
  729. buf.append("hours: ");
  730. buf.append(getExpressionSetSummary(hours));
  731. buf.append("\n");
  732. buf.append("daysOfMonth: ");
  733. buf.append(getExpressionSetSummary(daysOfMonth));
  734. buf.append("\n");
  735. buf.append("months: ");
  736. buf.append(getExpressionSetSummary(months));
  737. buf.append("\n");
  738. buf.append("daysOfWeek: ");
  739. buf.append(getExpressionSetSummary(daysOfWeek));
  740. buf.append("\n");
  741. buf.append("lastdayOfWeek: ");
  742. buf.append(lastdayOfWeek);
  743. buf.append("\n");
  744. buf.append("nearestWeekday: ");
  745. buf.append(nearestWeekday);
  746. buf.append("\n");
  747. buf.append("NthDayOfWeek: ");
  748. buf.append(nthdayOfWeek);
  749. buf.append("\n");
  750. buf.append("lastdayOfMonth: ");
  751. buf.append(lastdayOfMonth);
  752. buf.append("\n");
  753. buf.append("years: ");
  754. buf.append(getExpressionSetSummary(years));
  755. buf.append("\n");
  756. return buf.toString();
  757. }
  758. protected String getExpressionSetSummary(java.util.Set set) {
  759. if (set.contains(NO_SPEC)) {
  760. return "?";
  761. }
  762. if (set.contains(ALL_SPEC)) {
  763. return "*";
  764. }
  765. StringBuffer buf = new StringBuffer();
  766. Iterator itr = set.iterator();
  767. boolean first = true;
  768. while (itr.hasNext()) {
  769. Integer iVal = (Integer) itr.next();
  770. String val = iVal.toString();
  771. if (!first) {
  772. buf.append(",");
  773. }
  774. buf.append(val);
  775. first = false;
  776. }
  777. return buf.toString();
  778. }
  779. protected String getExpressionSetSummary(java.util.ArrayList list) {
  780. if (list.contains(NO_SPEC)) {
  781. return "?";
  782. }
  783. if (list.contains(ALL_SPEC)) {
  784. return "*";
  785. }
  786. StringBuffer buf = new StringBuffer();
  787. Iterator itr = list.iterator();
  788. boolean first = true;
  789. while (itr.hasNext()) {
  790. Integer iVal = (Integer) itr.next();
  791. String val = iVal.toString();
  792. if (!first) {
  793. buf.append(",");
  794. }
  795. buf.append(val);
  796. first = false;
  797. }
  798. return buf.toString();
  799. }
  800. protected int skipWhiteSpace(int i, String s) {
  801. for (; i < s.length() && (s.charAt(i) == ' ' || s.charAt(i) == '\t'); i++) {
  802. ;
  803. }
  804. return i;
  805. }
  806. protected int findNextWhiteSpace(int i, String s) {
  807. for (; i < s.length() && (s.charAt(i) != ' ' || s.charAt(i) != '\t'); i++) {
  808. ;
  809. }
  810. return i;
  811. }
  812. protected void addToSet(int val, int end, int incr, int type)
  813. throws ParseException {
  814. TreeSet set = getSet(type);
  815. if (type == SECOND || type == MINUTE) {
  816. if ((val < 0 || val > 59 || end > 59) && (val != ALL_SPEC_INT)) {
  817. throw new ParseException(
  818. "Minute and Second values must be between 0 and 59",
  819. -1);
  820. }
  821. } else if (type == HOUR) {
  822. if ((val < 0 || val > 23 || end > 23) && (val != ALL_SPEC_INT)) {
  823. throw new ParseException(
  824. "Hour values must be between 0 and 23", -1);
  825. }
  826. } else if (type == DAY_OF_MONTH) {
  827. if ((val < 1 || val > 31 || end > 31) && (val != ALL_SPEC_INT)
  828. && (val != NO_SPEC_INT)) {
  829. throw new ParseException(
  830. "Day of month values must be between 1 and 31", -1);
  831. }
  832. } else if (type == MONTH) {
  833. if ((val < 1 || val > 12 || end > 12) && (val != ALL_SPEC_INT)) {
  834. throw new ParseException(
  835. "Month values must be between 1 and 12", -1);
  836. }
  837. } else if (type == DAY_OF_WEEK) {
  838. if ((val == 0 || val > 7 || end > 7) && (val != ALL_SPEC_INT)
  839. && (val != NO_SPEC_INT)) {
  840. throw new ParseException(
  841. "Day-of-Week values must be between 1 and 7", -1);
  842. }
  843. }
  844. if ((incr == 0 || incr == -1) && val != ALL_SPEC_INT) {
  845. if (val != -1) {
  846. set.add(new Integer(val));
  847. } else {
  848. set.add(NO_SPEC);
  849. }
  850. return;
  851. }
  852. int startAt = val;
  853. int stopAt = end;
  854. if (val == ALL_SPEC_INT && incr <= 0) {
  855. incr = 1;
  856. set.add(ALL_SPEC); // put in a marker, but also fill values
  857. }
  858. if (type == SECOND || type == MINUTE) {
  859. if (stopAt == -1) {
  860. stopAt = 59;
  861. }
  862. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  863. startAt = 0;
  864. }
  865. } else if (type == HOUR) {
  866. if (stopAt == -1) {
  867. stopAt = 23;
  868. }
  869. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  870. startAt = 0;
  871. }
  872. } else if (type == DAY_OF_MONTH) {
  873. if (stopAt == -1) {
  874. stopAt = 31;
  875. }
  876. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  877. startAt = 1;
  878. }
  879. } else if (type == MONTH) {
  880. if (stopAt == -1) {
  881. stopAt = 12;
  882. }
  883. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  884. startAt = 1;
  885. }
  886. } else if (type == DAY_OF_WEEK) {
  887. if (stopAt == -1) {
  888. stopAt = 7;
  889. }
  890. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  891. startAt = 1;
  892. }
  893. } else if (type == YEAR) {
  894. if (stopAt == -1) {
  895. stopAt = CronTrigger.YEAR_TO_GIVEUP_SCHEDULING_AT;
  896. }
  897. if (startAt == -1 || startAt == ALL_SPEC_INT) {
  898. // needs to start at 1969 because timezones can make dates before 1970
  899. startAt = 1969;
  900. }
  901. }
  902. // if the end of the range is before the start, then we need to overflow into
  903. // the next day, month etc. This is done by adding the maximum amount for that
  904. // type, and using modulus max to determine the value being added.
  905. int max = -1;
  906. if (stopAt < startAt) {
  907. switch (type) {
  908. case SECOND : max = 60; break;
  909. case MINUTE : max = 60; break;
  910. case HOUR : max = 24; break;
  911. case MONTH : max = 12; break;
  912. case DAY_OF_WEEK : max = 7; break;
  913. case DAY_OF_MONTH : max = 31; break;
  914. case YEAR : throw new IllegalArgumentException("Start year must be less than stop year");
  915. default : throw new IllegalArgumentException("Unexpected type encountered");
  916. }
  917. stopAt += max;
  918. }
  919. for (int i = startAt; i <= stopAt; i += incr) {
  920. if (max == -1) {
  921. // ie: there's no max to overflow over
  922. set.add(new Integer(i));
  923. } else {
  924. // take the modulus to get the real value
  925. int i2 = i % max;
  926. // 1-indexed ranges should not include 0, and should include their max
  927. if (i2 == 0 && (type == MONTH || type == DAY_OF_WEEK || type == DAY_OF_MONTH) ) {
  928. i2 = max;
  929. }
  930. set.add(new Integer(i2));
  931. }
  932. }
  933. }
  934. protected TreeSet getSet(int type) {
  935. switch (type) {
  936. case SECOND:
  937. return seconds;
  938. case MINUTE:
  939. return minutes;
  940. case HOUR:
  941. return hours;
  942. case DAY_OF_MONTH:
  943. return daysOfMonth;
  944. case MONTH:
  945. return months;
  946. case DAY_OF_WEEK:
  947. return daysOfWeek;
  948. case YEAR:
  949. return years;
  950. default:
  951. return null;
  952. }
  953. }
  954. protected ValueSet getValue(int v, String s, int i) {
  955. char c = s.charAt(i);
  956. String s1 = String.valueOf(v);
  957. while (c >= '0' && c <= '9') {
  958. s1 += c;
  959. i++;
  960. if (i >= s.length()) {
  961. break;
  962. }
  963. c = s.charAt(i);
  964. }
  965. ValueSet val = new ValueSet();
  966. val.pos = (i < s.length()) ? i : i + 1;
  967. val.value = Integer.parseInt(s1);
  968. return val;
  969. }
  970. protected int getNumericValue(String s, int i) {
  971. int endOfVal = findNextWhiteSpace(i, s);
  972. String val = s.substring(i, endOfVal);
  973. return Integer.parseInt(val);
  974. }
  975. protected int getMonthNumber(String s) {
  976. Integer integer = (Integer) monthMap.get(s);
  977. if (integer == null) {
  978. return -1;
  979. }
  980. return integer.intValue();
  981. }
  982. protected int getDayOfWeekNumber(String s) {
  983. Integer integer = (Integer) dayMap.get(s);
  984. if (integer == null) {
  985. return -1;
  986. }
  987. return integer.intValue();
  988. }
  989. ////////////////////////////////////////////////////////////////////////////
  990. //
  991. // Computation Functions
  992. //
  993. ////////////////////////////////////////////////////////////////////////////
  994. protected Date getTimeAfter(Date afterTime) {
  995. Calendar cl = Calendar.getInstance(getTimeZone());
  996. // move ahead one second, since we're computing the time *after* the
  997. // given time
  998. afterTime = new Date(afterTime.getTime() + 1000);
  999. // CronTrigger does not deal with milliseconds
  1000. cl.setTime(afterTime);
  1001. cl.set(Calendar.MILLISECOND, 0);
  1002. boolean gotOne = false;
  1003. // loop until we've computed the next time, or we've past the endTime
  1004. while (!gotOne) {
  1005. //if (endTime != null && cl.getTime().after(endTime)) return null;
  1006. if(cl.get(Calendar.YEAR) > 2999) { // prevent endless loop...
  1007. return null;
  1008. }
  1009. SortedSet st = null;
  1010. int t = 0;
  1011. int sec = cl.get(Calendar.SECOND);
  1012. int min = cl.get(Calendar.MINUTE);
  1013. // get second.................................................
  1014. st = seconds.tailSet(new Integer(sec));
  1015. if (st != null && st.size() != 0) {
  1016. sec = ((Integer) st.first()).intValue();
  1017. } else {
  1018. sec = ((Integer) seconds.first()).intValue();
  1019. min++;
  1020. cl.set(Calendar.MINUTE, min);
  1021. }
  1022. cl.set(Calendar.SECOND, sec);
  1023. min = cl.get(Calendar.MINUTE);
  1024. int hr = cl.get(Calendar.HOUR_OF_DAY);
  1025. t = -1;
  1026. // get minute.................................................
  1027. st = minutes.tailSet(new Integer(min));
  1028. if (st != null && st.size() != 0) {
  1029. t = min;
  1030. min = ((Integer) st.first()).intValue();
  1031. } else {
  1032. min = ((Integer) minutes.first()).intValue();
  1033. hr++;
  1034. }
  1035. if (min != t) {
  1036. cl.set(Calendar.SECOND, 0);
  1037. cl.set(Calendar.MINUTE, min);
  1038. setCalendarHour(cl, hr);
  1039. continue;
  1040. }
  1041. cl.set(Calendar.MINUTE, min);
  1042. hr = cl.get(Calendar.HOUR_OF_DAY);
  1043. int day = cl.get(Calendar.DAY_OF_MONTH);
  1044. t = -1;
  1045. // get hour...................................................
  1046. st = hours.tailSet(new Integer(hr));
  1047. if (st != null && st.size() != 0) {
  1048. t = hr;
  1049. hr = ((Integer) st.first()).intValue();
  1050. } else {
  1051. hr = ((Integer) hours.first()).intValue();
  1052. day++;
  1053. }
  1054. if (hr != t) {
  1055. cl.set(Calendar.SECOND, 0);
  1056. cl.set(Calendar.MINUTE, 0);
  1057. cl.set(Calendar.DAY_OF_MONTH, day);
  1058. setCalendarHour(cl, hr);
  1059. continue;
  1060. }
  1061. cl.set(Calendar.HOUR_OF_DAY, hr);
  1062. day = cl.get(Calendar.DAY_OF_MONTH);
  1063. int mon = cl.get(Calendar.MONTH) + 1;
  1064. // '+ 1' because calendar is 0-based for this field, and we are
  1065. // 1-based
  1066. t = -1;
  1067. int tmon = mon;
  1068. // get day...................................................
  1069. boolean dayOfMSpec = !daysOfMonth.contains(NO_SPEC);
  1070. boolean dayOfWSpec = !daysOfWeek.contains(NO_SPEC);
  1071. if (dayOfMSpec && !dayOfWSpec) { // get day by day of month rule
  1072. st = daysOfMonth.tailSet(new Integer(day));
  1073. if (lastdayOfMonth) {
  1074. if(!nearestWeekday) {
  1075. t = day;
  1076. day = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1077. } else {
  1078. t = day;
  1079. day = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1080. java.util.Calendar tcal = java.util.Calendar.getInstance(getTimeZone());
  1081. tcal.set(Calendar.SECOND, 0);
  1082. tcal.set(Calendar.MINUTE, 0);
  1083. tcal.set(Calendar.HOUR_OF_DAY, 0);
  1084. tcal.set(Calendar.DAY_OF_MONTH, day);
  1085. tcal.set(Calendar.MONTH, mon - 1);
  1086. tcal.set(Calendar.YEAR, cl.get(Calendar.YEAR));
  1087. int ldom = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1088. int dow = tcal.get(Calendar.DAY_OF_WEEK);
  1089. if(dow == Calendar.SATURDAY && day == 1) {
  1090. day += 2;
  1091. } else if(dow == Calendar.SATURDAY) {
  1092. day -= 1;
  1093. } else if(dow == Calendar.SUNDAY && day == ldom) {
  1094. day -= 2;
  1095. } else if(dow == Calendar.SUNDAY) {
  1096. day += 1;
  1097. }
  1098. tcal.set(Calendar.SECOND, sec);
  1099. tcal.set(Calendar.MINUTE, min);
  1100. tcal.set(Calendar.HOUR_OF_DAY, hr);
  1101. tcal.set(Calendar.DAY_OF_MONTH, day);
  1102. tcal.set(Calendar.MONTH, mon - 1);
  1103. Date nTime = tcal.getTime();
  1104. if(nTime.before(afterTime)) {
  1105. day = 1;
  1106. mon++;
  1107. }
  1108. }
  1109. } else if(nearestWeekday) {
  1110. t = day;
  1111. day = ((Integer) daysOfMonth.first()).intValue();
  1112. java.util.Calendar tcal = java.util.Calendar.getInstance(getTimeZone());
  1113. tcal.set(Calendar.SECOND, 0);
  1114. tcal.set(Calendar.MINUTE, 0);
  1115. tcal.set(Calendar.HOUR_OF_DAY, 0);
  1116. tcal.set(Calendar.DAY_OF_MONTH, day);
  1117. tcal.set(Calendar.MONTH, mon - 1);
  1118. tcal.set(Calendar.YEAR, cl.get(Calendar.YEAR));
  1119. int ldom = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1120. int dow = tcal.get(Calendar.DAY_OF_WEEK);
  1121. if(dow == Calendar.SATURDAY && day == 1) {
  1122. day += 2;
  1123. } else if(dow == Calendar.SATURDAY) {
  1124. day -= 1;
  1125. } else if(dow == Calendar.SUNDAY && day == ldom) {
  1126. day -= 2;
  1127. } else if(dow == Calendar.SUNDAY) {
  1128. day += 1;
  1129. }
  1130. tcal.set(Calendar.SECOND, sec);
  1131. tcal.set(Calendar.MINUTE, min);
  1132. tcal.set(Calendar.HOUR_OF_DAY, hr);
  1133. tcal.set(Calendar.DAY_OF_MONTH, day);
  1134. tcal.set(Calendar.MONTH, mon - 1);
  1135. Date nTime = tcal.getTime();
  1136. if(nTime.before(afterTime)) {
  1137. day = ((Integer) daysOfMonth.first()).intValue();
  1138. mon++;
  1139. }
  1140. } else if (st != null && st.size() != 0) {
  1141. t = day;
  1142. day = ((Integer) st.first()).intValue();
  1143. // make sure we don't over-run a short month, such as february
  1144. int lastDay = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1145. if (day > lastDay) {
  1146. day = ((Integer) daysOfMonth.first()).intValue();
  1147. mon++;
  1148. }
  1149. } else {
  1150. day = ((Integer) daysOfMonth.first()).intValue();
  1151. mon++;
  1152. }
  1153. if (day != t || mon != tmon) {
  1154. cl.set(Calendar.SECOND, 0);
  1155. cl.set(Calendar.MINUTE, 0);
  1156. cl.set(Calendar.HOUR_OF_DAY, 0);
  1157. cl.set(Calendar.DAY_OF_MONTH, day);
  1158. cl.set(Calendar.MONTH, mon - 1);
  1159. // '- 1' because calendar is 0-based for this field, and we
  1160. // are 1-based
  1161. continue;
  1162. }
  1163. } else if (dayOfWSpec && !dayOfMSpec) { // get day by day of week rule
  1164. if (lastdayOfWeek) { // are we looking for the last XXX day of
  1165. // the month?
  1166. int dow = ((Integer) daysOfWeek.first()).intValue(); // desired
  1167. // d-o-w
  1168. int cDow = cl.get(Calendar.DAY_OF_WEEK); // current d-o-w
  1169. int daysToAdd = 0;
  1170. if (cDow < dow) {
  1171. daysToAdd = dow - cDow;
  1172. }
  1173. if (cDow > dow) {
  1174. daysToAdd = dow + (7 - cDow);
  1175. }
  1176. int lDay = getLastDayOfMonth(mon, cl.get(Calendar.YEAR));
  1177. if (day + daysToAdd > lDay) { // did we already miss the
  1178. // last one?
  1179. cl.set(Calendar.SECOND, 0);
  1180. cl.set(Calendar.MINUTE, 0);
  1181. cl.set(Calendar.HOUR_OF_DAY, 0);
  1182. cl.set(Calendar.DAY_OF_MONTH, 1);
  1183. cl.set(Calendar.MONTH, mon);
  1184. // no '- 1' here because we are promoting the month
  1185. continue;
  1186. }
  1187. // find date of last occurance of this day in this month...
  1188. while ((day + daysToAdd + 7) <= lDay) {
  1189. daysToAdd += 7;
  1190. }
  1191. day += daysToAdd;
  1192. if (daysToAdd > 0) {
  1193. cl.set(Calendar.SECOND, 0);
  1194. cl.set(Calendar.MINUTE, 0);
  1195. cl.set(Calendar.HOUR_OF_DAY, 0);
  1196. cl.set(Calendar.DAY_OF_MONTH, day);
  1197. cl.set(Calendar.MONTH, mon - 1);
  1198. // '- 1' here because we are not promoting the month
  1199. continue;
  1200. }
  1201. } else if (nthdayOfWeek != 0) {
  1202. // are we looking for the Nth XXX day in the month?
  1203. int dow = ((Integer) daysOfWeek.first()).intValue(); // desired
  1204. // d-o-w
  1205. int

Large files files are truncated, but you can click here to view the full file