PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/engine_java/000_Engine_Core/lib/quartz-1.6.6/src/java/org/quartz/CronExpression.java

http://cellengine.googlecode.com/
Java | 1581 lines | 1111 code | 164 blank | 306 comment | 475 complexity | dff67e26f2229fb566ecf812af832bdd MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, BSD-3-Clause, AGPL-3.0, LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception, CPL-1.0

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

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

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