PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/brzy-scheduler/src/main/java/org/brzy/mod/scheduler/CronExpression.java

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

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