1 2 18 19 22 package org.quartz; 23 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.TimeZone ; 29 30 47 public class TriggerUtils { 48 49 56 57 public static final int SUNDAY = 1; 58 59 public static final int MONDAY = 2; 60 61 public static final int TUESDAY = 3; 62 63 public static final int WEDNESDAY = 4; 64 65 public static final int THURSDAY = 5; 66 67 public static final int FRIDAY = 6; 68 69 public static final int SATURDAY = 7; 70 71 public static final int LAST_DAY_OF_MONTH = -1; 72 73 public static final long MILLISECONDS_IN_MINUTE = 60l * 1000l; 74 75 public static final long MILLISECONDS_IN_HOUR = 60l * 60l * 1000l; 76 77 public static final long SECONDS_IN_DAY = 24l * 60l * 60L; 78 79 public static final long MILLISECONDS_IN_DAY = SECONDS_IN_DAY * 1000l; 80 81 84 private TriggerUtils() { 85 } 86 87 94 95 private static void validateDayOfWeek(int dayOfWeek) { 96 if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) { 97 throw new IllegalArgumentException ("Invalid day of week."); 98 } 99 } 100 101 private static void validateHour(int hour) { 102 if (hour < 0 || hour > 23) { 103 throw new IllegalArgumentException ( 104 "Invalid hour (must be >= 0 and <= 23)."); 105 } 106 } 107 108 private static void validateMinute(int minute) { 109 if (minute < 0 || minute > 59) { 110 throw new IllegalArgumentException ( 111 "Invalid minute (must be >= 0 and <= 59)."); 112 } 113 } 114 115 private static void validateSecond(int second) { 116 if (second < 0 || second > 59) { 117 throw new IllegalArgumentException ( 118 "Invalid second (must be >= 0 and <= 59)."); 119 } 120 } 121 122 private static void validateDayOfMonth(int day) { 123 if ((day < 1 || day > 31) && day != LAST_DAY_OF_MONTH) { 124 throw new IllegalArgumentException ("Invalid day of month."); 125 } 126 } 127 128 private static void validateMonth(int month) { 129 if (month < 1 || month > 12) { 130 throw new IllegalArgumentException ( 131 "Invalid month (must be >= 1 and <= 12."); 132 } 133 } 134 135 private static void validateYear(int year) { 136 if (year < 1970 || year > 2099) { 137 throw new IllegalArgumentException ( 138 "Invalid year (must be >= 1970 and <= 2099."); 139 } 140 } 141 142 151 public static void setTriggerIdentity(Trigger trig, String name) { 152 setTriggerIdentity(trig, name, Scheduler.DEFAULT_GROUP); 153 } 154 155 165 public static void setTriggerIdentity( 166 Trigger trig, String name, String group) { 167 trig.setName(name); 168 trig.setGroup(group); 169 } 170 171 185 public static Trigger makeDailyTrigger(int hour, int minute) { 186 validateHour(hour); 187 validateMinute(minute); 188 189 CronTrigger trig = new CronTrigger(); 190 191 try { 192 trig.setCronExpression("0 " + minute + " " + hour + " ? * *"); 193 } catch (Exception ignore) { 194 return null; 195 } 196 197 trig.setStartTime(new Date ()); 198 199 return trig; 200 } 201 202 217 public static Trigger makeDailyTrigger( 218 String trigName, int hour, int minute) { 219 Trigger trig = makeDailyTrigger(hour, minute); 220 trig.setName(trigName); 221 return trig; 222 } 223 224 247 public static Trigger makeWeeklyTrigger( 248 int dayOfWeek, int hour, int minute) { 249 validateDayOfWeek(dayOfWeek); 250 validateHour(hour); 251 validateMinute(minute); 252 253 CronTrigger trig = new CronTrigger(); 254 255 try { 256 trig.setCronExpression("0 " + minute + " " + hour + " ? * " 257 + dayOfWeek); 258 } catch (Exception ignore) { 259 return null; 260 } 261 262 trig.setStartTime(new Date ()); 263 264 return trig; 265 } 266 267 291 public static Trigger makeWeeklyTrigger( 292 String trigName, int dayOfWeek, int hour, int minute) { 293 Trigger trig = makeWeeklyTrigger(dayOfWeek, hour, minute); 294 trig.setName(trigName); 295 return trig; 296 } 297 298 299 321 public static Trigger makeMonthlyTrigger( 322 int dayOfMonth, int hour, int minute) { 323 validateDayOfMonth(dayOfMonth); 324 validateHour(hour); 325 validateMinute(minute); 326 327 CronTrigger trig = new CronTrigger(); 328 329 try { 330 if (dayOfMonth != LAST_DAY_OF_MONTH) { 331 trig.setCronExpression("0 " + minute + " " + hour + " " + dayOfMonth + " * ?"); 332 } else { 333 trig.setCronExpression("0 " + minute + " " + hour + " L * ?"); 334 } 335 } catch (Exception ignore) { 336 return null; 337 } 338 339 trig.setStartTime(new Date ()); 340 341 return trig; 342 } 343 344 367 public static Trigger makeMonthlyTrigger( 368 String trigName, int dayOfMonth, int hour, int minute) { 369 Trigger trig = makeMonthlyTrigger(dayOfMonth, hour, minute); 370 trig.setName(trigName); 371 return trig; 372 } 373 374 393 394 409 public static Trigger makeImmediateTrigger( 410 int repeatCount, long repeatInterval) { 411 SimpleTrigger trig = new SimpleTrigger(); 412 trig.setStartTime( new Date () ); 413 trig.setRepeatCount(repeatCount); 414 trig.setRepeatInterval(repeatInterval); 415 return trig; 416 } 417 418 434 public static Trigger makeImmediateTrigger( 435 String trigName, int repeatCount, long repeatInterval) { 436 Trigger trig = makeImmediateTrigger(repeatCount, repeatInterval); 437 trig.setName(trigName); 438 return trig; 439 } 440 441 452 public static Trigger makeSecondlyTrigger() { 453 return makeSecondlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 454 } 455 456 469 public static Trigger makeSecondlyTrigger(String trigName) { 470 return makeSecondlyTrigger( 471 trigName, 1, SimpleTrigger.REPEAT_INDEFINITELY); 472 } 473 474 475 488 public static Trigger makeSecondlyTrigger(int intervalInSeconds) { 489 return makeSecondlyTrigger( 490 intervalInSeconds, SimpleTrigger.REPEAT_INDEFINITELY); 491 } 492 493 508 public static Trigger makeSecondlyTrigger( 509 int intervalInSeconds, int repeatCount) { 510 SimpleTrigger trig = new SimpleTrigger(); 511 512 trig.setRepeatInterval(intervalInSeconds * 1000l); 513 trig.setRepeatCount(repeatCount); 514 trig.setStartTime(new Date ()); 515 516 return trig; 517 } 518 519 535 public static Trigger makeSecondlyTrigger( 536 String trigName, int intervalInSeconds, int repeatCount) { 537 Trigger trig = makeSecondlyTrigger(intervalInSeconds, repeatCount); 538 trig.setName(trigName); 539 return trig; 540 } 541 542 554 public static Trigger makeMinutelyTrigger() { 555 return makeMinutelyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 556 } 557 558 571 public static Trigger makeMinutelyTrigger(String trigName) { 572 return makeMinutelyTrigger( 573 trigName, 1, SimpleTrigger.REPEAT_INDEFINITELY); 574 } 575 576 589 public static Trigger makeMinutelyTrigger(int intervalInMinutes) { 590 return makeMinutelyTrigger( 591 intervalInMinutes, SimpleTrigger.REPEAT_INDEFINITELY); 592 } 593 594 609 public static Trigger makeMinutelyTrigger( 610 int intervalInMinutes, int repeatCount) { 611 SimpleTrigger trig = new SimpleTrigger(); 612 613 trig.setRepeatInterval(intervalInMinutes * MILLISECONDS_IN_MINUTE); 614 trig.setRepeatCount(repeatCount); 615 616 trig.setStartTime(new Date ()); 617 618 return trig; 619 } 620 621 637 public static Trigger makeMinutelyTrigger( 638 String trigName, int intervalInMinutes, int repeatCount) { 639 Trigger trig = makeMinutelyTrigger(intervalInMinutes, repeatCount); 640 trig.setName(trigName); 641 return trig; 642 } 643 644 656 public static Trigger makeHourlyTrigger() { 657 return makeHourlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 658 } 659 660 673 public static Trigger makeHourlyTrigger(String trigName) { 674 return makeHourlyTrigger( 675 trigName, 1, SimpleTrigger.REPEAT_INDEFINITELY); 676 } 677 678 691 public static Trigger makeHourlyTrigger(int intervalInHours) { 692 return makeHourlyTrigger( 693 intervalInHours, SimpleTrigger.REPEAT_INDEFINITELY); 694 } 695 696 711 public static Trigger makeHourlyTrigger( 712 int intervalInHours, int repeatCount) { 713 SimpleTrigger trig = new SimpleTrigger(); 714 715 trig.setRepeatInterval(intervalInHours * MILLISECONDS_IN_HOUR); 716 trig.setRepeatCount(repeatCount); 717 718 trig.setStartTime(new Date ()); 719 720 return trig; 721 } 722 723 739 public static Trigger makeHourlyTrigger( 740 String trigName, int intervalInHours, int repeatCount) { 741 Trigger trig =makeHourlyTrigger(intervalInHours, repeatCount); 742 trig.setName(trigName); 743 return trig; 744 } 745 746 763 public static Date getEvenHourDate(Date date) { 764 if (date == null) { 765 date = new Date (); 766 } 767 768 Calendar c = Calendar.getInstance(); 769 c.setTime(date); 770 c.setLenient(true); 771 772 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 773 c.set(Calendar.MINUTE, 0); 774 c.set(Calendar.SECOND, 0); 775 c.set(Calendar.MILLISECOND, 0); 776 777 return c.getTime(); 778 } 779 780 796 public static Date getEvenHourDateBefore(Date date) { 797 if (date == null) { 798 date = new Date (); 799 } 800 801 Calendar c = Calendar.getInstance(); 802 c.setTime(date); 803 804 c.set(Calendar.MINUTE, 0); 805 c.set(Calendar.SECOND, 0); 806 c.set(Calendar.MILLISECOND, 0); 807 808 return c.getTime(); 809 } 810 811 828 public static Date getEvenMinuteDate(Date date) { 829 if (date == null) { 830 date = new Date (); 831 } 832 833 Calendar c = Calendar.getInstance(); 834 c.setTime(date); 835 c.setLenient(true); 836 837 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 838 c.set(Calendar.SECOND, 0); 839 c.set(Calendar.MILLISECOND, 0); 840 841 return c.getTime(); 842 } 843 844 860 public static Date getEvenMinuteDateBefore(Date date) { 861 if (date == null) { 862 date = new Date (); 863 } 864 865 Calendar c = Calendar.getInstance(); 866 c.setTime(date); 867 868 c.set(Calendar.SECOND, 0); 869 c.set(Calendar.MILLISECOND, 0); 870 871 return c.getTime(); 872 } 873 874 885 public static Date getEvenSecondDate(Date date) { 886 if (date == null) { 887 date = new Date (); 888 } 889 890 Calendar c = Calendar.getInstance(); 891 c.setTime(date); 892 c.setLenient(true); 893 894 c.set(Calendar.SECOND, c.get(Calendar.SECOND) + 1); 895 c.set(Calendar.MILLISECOND, 0); 896 897 return c.getTime(); 898 } 899 900 916 public static Date getEvenSecondDateBefore(Date date) { 917 if (date == null) { 918 date = new Date (); 919 } 920 921 Calendar c = Calendar.getInstance(); 922 c.setTime(date); 923 924 c.set(Calendar.MILLISECOND, 0); 925 926 return c.getTime(); 927 } 928 929 1022 public static Date getNextGivenMinuteDate(Date date, int minuteBase) { 1023 if (minuteBase < 0 || minuteBase > 59) { 1024 throw new IllegalArgumentException ( 1025 "minuteBase must be >=0 and <= 59"); 1026 } 1027 1028 if (date == null) { 1029 date = new Date (); 1030 } 1031 1032 Calendar c = Calendar.getInstance(); 1033 c.setTime(date); 1034 c.setLenient(true); 1035 1036 if (minuteBase == 0) { 1037 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 1038 c.set(Calendar.MINUTE, 0); 1039 c.set(Calendar.SECOND, 0); 1040 c.set(Calendar.MILLISECOND, 0); 1041 1042 return c.getTime(); 1043 } 1044 1045 int minute = c.get(Calendar.MINUTE); 1046 1047 int arItr = minute / minuteBase; 1048 1049 int nextMinuteOccurance = minuteBase * (arItr + 1); 1050 1051 if (nextMinuteOccurance < 60) { 1052 c.set(Calendar.MINUTE, nextMinuteOccurance); 1053 c.set(Calendar.SECOND, 0); 1054 c.set(Calendar.MILLISECOND, 0); 1055 1056 return c.getTime(); 1057 } else { 1058 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 1059 c.set(Calendar.MINUTE, 0); 1060 c.set(Calendar.SECOND, 0); 1061 c.set(Calendar.MILLISECOND, 0); 1062 1063 return c.getTime(); 1064 } 1065 } 1066 1067 1086 public static Date getNextGivenSecondDate(Date date, int secondBase) { 1087 if (secondBase < 0 || secondBase > 59) { 1088 throw new IllegalArgumentException ( 1089 "secondBase must be >=0 and <= 59"); 1090 } 1091 1092 if (date == null) { 1093 date = new Date (); 1094 } 1095 1096 Calendar c = Calendar.getInstance(); 1097 c.setTime(date); 1098 c.setLenient(true); 1099 1100 if (secondBase == 0) { 1101 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 1102 c.set(Calendar.SECOND, 0); 1103 c.set(Calendar.MILLISECOND, 0); 1104 1105 return c.getTime(); 1106 } 1107 1108 int second = c.get(Calendar.SECOND); 1109 1110 int arItr = second / secondBase; 1111 1112 int nextSecondOccurance = secondBase * (arItr + 1); 1113 1114 if (nextSecondOccurance < 60) { 1115 c.set(Calendar.SECOND, nextSecondOccurance); 1116 c.set(Calendar.MILLISECOND, 0); 1117 1118 return c.getTime(); 1119 } else { 1120 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 1121 c.set(Calendar.SECOND, 0); 1122 c.set(Calendar.MILLISECOND, 0); 1123 1124 return c.getTime(); 1125 } 1126 } 1127 1128 1142 public static Date getDateOf(int second, int minute, int hour) { 1143 validateSecond(second); 1144 validateMinute(minute); 1145 validateHour(hour); 1146 1147 Date date = new Date (); 1148 1149 Calendar c = Calendar.getInstance(); 1150 c.setTime(date); 1151 c.setLenient(true); 1152 1153 c.set(Calendar.HOUR_OF_DAY, hour); 1154 c.set(Calendar.MINUTE, minute); 1155 c.set(Calendar.SECOND, second); 1156 c.set(Calendar.MILLISECOND, 0); 1157 1158 return c.getTime(); 1159 } 1160 1161 1179 public static Date getDateOf(int second, int minute, int hour, 1180 int dayOfMonth, int month) { 1181 validateSecond(second); 1182 validateMinute(minute); 1183 validateHour(hour); 1184 validateDayOfMonth(dayOfMonth); 1185 validateMonth(month); 1186 1187 Date date = new Date (); 1188 1189 Calendar c = Calendar.getInstance(); 1190 c.setTime(date); 1191 1192 c.set(Calendar.MONTH, month - 1); 1193 c.set(Calendar.DAY_OF_MONTH, dayOfMonth); 1194 c.set(Calendar.HOUR_OF_DAY, hour); 1195 c.set(Calendar.MINUTE, minute); 1196 c.set(Calendar.SECOND, second); 1197 c.set(Calendar.MILLISECOND, 0); 1198 1199 return c.getTime(); 1200 } 1201 1202 1222 public static Date getDateOf(int second, int minute, int hour, 1223 int dayOfMonth, int month, int year) { 1224 validateSecond(second); 1225 validateMinute(minute); 1226 validateHour(hour); 1227 validateDayOfMonth(dayOfMonth); 1228 validateMonth(month); 1229 validateYear(year); 1230 1231 Date date = new Date (); 1232 1233 Calendar c = Calendar.getInstance(); 1234 c.setTime(date); 1235 1236 c.set(Calendar.YEAR, year); 1237 c.set(Calendar.MONTH, month - 1); 1238 c.set(Calendar.DAY_OF_MONTH, dayOfMonth); 1239 c.set(Calendar.HOUR_OF_DAY, hour); 1240 c.set(Calendar.MINUTE, minute); 1241 c.set(Calendar.SECOND, second); 1242 c.set(Calendar.MILLISECOND, 0); 1243 1244 return c.getTime(); 1245 } 1246 1247 1261 public static List computeFireTimes(Trigger trigg, org.quartz.Calendar cal, 1262 int numTimes) { 1263 LinkedList lst = new LinkedList (); 1264 1265 Trigger t = (Trigger) trigg.clone(); 1266 1267 if (t.getNextFireTime() == null) { 1268 t.computeFirstFireTime(cal); 1269 } 1270 1271 for (int i = 0; i < numTimes; i++) { 1272 Date d = t.getNextFireTime(); 1273 if (d != null) { 1274 lst.add(d); 1275 t.triggered(cal); 1276 } else { 1277 break; 1278 } 1279 } 1280 1281 return java.util.Collections.unmodifiableList(lst); 1282 } 1283 1284 1307 public static List computeFireTimesBetween(Trigger trigg, 1308 org.quartz.Calendar cal, Date from, Date to) { 1309 LinkedList lst = new LinkedList (); 1310 1311 Trigger t = (Trigger) trigg.clone(); 1312 1313 if (t.getNextFireTime() == null) { 1314 t.setStartTime(from); 1315 t.setEndTime(to); 1316 t.computeFirstFireTime(cal); 1317 } 1318 1319 while (true) { 1322 Date d = t.getNextFireTime(); 1323 if (d != null) { 1324 if (d.before(from)) { 1325 t.triggered(cal); 1326 continue; 1327 } 1328 if (d.after(to)) { 1329 break; 1330 } 1331 lst.add(d); 1332 t.triggered(cal); 1333 } else { 1334 break; 1335 } 1336 } 1337 1338 return java.util.Collections.unmodifiableList(lst); 1339 } 1340 1341 1351 public static Date translateTime(Date date, TimeZone src, TimeZone dest) { 1352 1353 Date newDate = new Date (); 1354 1355 int offset = ( 1356 getOffset(date.getTime(), dest) - getOffset(date.getTime(), src)); 1357 1358 newDate.setTime(date.getTime() - offset); 1359 1360 return newDate; 1361 } 1362 1363 1376 public static int getOffset(long date, TimeZone tz) { 1377 1378 if (tz.inDaylightTime(new Date (date))) { 1379 return tz.getRawOffset() + getDSTSavings(tz); 1380 } 1381 1382 return tz.getRawOffset(); 1383 } 1384 1385 1394 public static int getDSTSavings(TimeZone tz) { 1395 1396 if (tz.useDaylightTime()) { 1397 return 3600000; 1398 } 1399 return 0; 1400 } 1401} 1402 | Popular Tags |