1 2 18 19 22 package org.quartz.helpers; 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 import org.quartz.CronTrigger; 31 import org.quartz.Scheduler; 32 import org.quartz.SimpleTrigger; 33 import org.quartz.Trigger; 34 35 54 public class TriggerUtils { 55 56 63 64 public static final int SUNDAY = 1; 65 66 public static final int MONDAY = 2; 67 68 public static final int TUESDAY = 3; 69 70 public static final int WEDNESDAY = 4; 71 72 public static final int THURSDAY = 5; 73 74 public static final int FRIDAY = 6; 75 76 public static final int SATURDAY = 7; 77 78 public static final int LAST_DAY_OF_MONTH = -1; 79 80 public static final long MILLISECONDS_IN_MINUTE = 60l * 1000l; 81 82 public static final long MILLISECONDS_IN_HOUR = 60l * 60l * 1000l; 83 84 public static final long SECONDS_IN_DAY = 24l * 60l * 60L; 85 86 public static final long MILLISECONDS_IN_DAY = SECONDS_IN_DAY * 1000l; 87 88 91 private TriggerUtils() { 92 } 93 94 101 102 private static void validateDayOfWeek(int dayOfWeek) { 103 if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) { 104 throw new IllegalArgumentException ("Invalid day of week."); 105 } 106 } 107 108 private static void validateHour(int hour) { 109 if (hour < 0 || hour > 23) { 110 throw new IllegalArgumentException ( 111 "Invalid hour (must be >= 0 and <= 23)."); 112 } 113 } 114 115 private static void validateMinute(int minute) { 116 if (minute < 0 || minute > 59) { 117 throw new IllegalArgumentException ( 118 "Invalid minute (must be >= 0 and <= 59)."); 119 } 120 } 121 122 private static void validateSecond(int second) { 123 if (second < 0 || second > 59) { 124 throw new IllegalArgumentException ( 125 "Invalid second (must be >= 0 and <= 59)."); 126 } 127 } 128 129 private static void validateDayOfMonth(int day) { 130 if ((day < 1 || day > 31) && day != LAST_DAY_OF_MONTH) { 131 throw new IllegalArgumentException ("Invalid day of month."); 132 } 133 } 134 135 private static void validateMonth(int month) { 136 if (month < 1 || month > 12) { 137 throw new IllegalArgumentException ( 138 "Invalid month (must be >= 1 and <= 12."); 139 } 140 } 141 142 private static void validateYear(int year) { 143 if (year < 1970 || year > 2099) { 144 throw new IllegalArgumentException ( 145 "Invalid year (must be >= 1970 and <= 2099."); 146 } 147 } 148 149 158 public static void setTriggerIdentity(Trigger trig, String name) { 159 trig.setName(name); 160 trig.setGroup(Scheduler.DEFAULT_GROUP); 161 } 162 163 172 public static void setTriggerIdentity(Trigger trig, String name, 173 String group) { 174 trig.setName(name); 175 trig.setGroup(group); 176 } 177 178 196 public static Trigger makeDailyTrigger(int hour, int minute) { 197 validateHour(hour); 198 validateMinute(minute); 199 200 CronTrigger trig = new CronTrigger(); 201 202 try { 203 trig.setCronExpression("0 " + minute + " " + hour + " ? * *"); 204 } catch (Exception ignore) { 205 return null; 206 } 207 208 return trig; 209 } 210 211 239 public static Trigger makeWeeklyTrigger(int dayOfWeek, int hour, int minute) { 240 validateDayOfWeek(dayOfWeek); 241 validateHour(hour); 242 validateMinute(minute); 243 244 CronTrigger trig = new CronTrigger(); 245 246 try { 247 trig.setCronExpression("0 " + minute + " " + hour + " ? * " 248 + dayOfWeek); 249 } catch (Exception ignore) { 250 return null; 251 } 252 253 return trig; 254 } 255 256 283 public static Trigger makeMonthlyTrigger(int dayOfMonth, int hour, 284 int minute) { 285 validateDayOfMonth(dayOfMonth); 286 validateHour(hour); 287 validateMinute(minute); 288 289 CronTrigger trig = new CronTrigger(); 290 291 try { 292 if (dayOfMonth != LAST_DAY_OF_MONTH) { 293 trig.setCronExpression("0 " + minute + " " + hour + " " + dayOfMonth + " * ?"); 294 } else { 295 trig.setCronExpression("0 " + minute + " " + hour + " L * ?"); 296 } 297 } catch (Exception ignore) { 298 return null; 299 } 300 301 return trig; 302 } 303 304 323 324 338 public static Trigger makeSecondlyTrigger() { 339 return makeSecondlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 340 } 341 342 358 public static Trigger makeSecondlyTrigger(int intervalInSeconds) { 359 return makeSecondlyTrigger(intervalInSeconds, 360 SimpleTrigger.REPEAT_INDEFINITELY); 361 } 362 363 382 public static Trigger makeSecondlyTrigger(int intervalInSeconds, 383 int repeatCount) { 384 SimpleTrigger trig = new SimpleTrigger(); 385 386 trig.setRepeatInterval(intervalInSeconds * 1000l); 387 trig.setRepeatCount(repeatCount); 388 389 return trig; 390 } 391 392 406 public static Trigger makeMinutelyTrigger() { 407 return makeMinutelyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 408 } 409 410 426 public static Trigger makeMinutelyTrigger(int intervalInMinutes) { 427 return makeMinutelyTrigger(intervalInMinutes, 428 SimpleTrigger.REPEAT_INDEFINITELY); 429 } 430 431 450 public static Trigger makeMinutelyTrigger(int intervalInMinutes, 451 int repeatCount) { 452 SimpleTrigger trig = new SimpleTrigger(); 453 454 trig.setRepeatInterval(intervalInMinutes * MILLISECONDS_IN_MINUTE); 455 trig.setRepeatCount(repeatCount); 456 457 return trig; 458 } 459 460 474 public static Trigger makeHourlyTrigger() { 475 return makeHourlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY); 476 } 477 478 494 public static Trigger makeHourlyTrigger(int intervalInHours) { 495 return makeHourlyTrigger(intervalInHours, 496 SimpleTrigger.REPEAT_INDEFINITELY); 497 } 498 499 518 public static Trigger makeHourlyTrigger(int intervalInHours, int repeatCount) { 519 SimpleTrigger trig = new SimpleTrigger(); 520 521 trig.setRepeatInterval(intervalInHours * MILLISECONDS_IN_HOUR); 522 trig.setRepeatCount(repeatCount); 523 524 return trig; 525 } 526 527 546 public static Date getEvenHourDate(Date date) { 547 if (date == null) { 548 date = new Date (); 549 } 550 551 Calendar c = Calendar.getInstance(); 552 c.setTime(date); 553 c.setLenient(true); 554 555 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 556 c.set(Calendar.MINUTE, 0); 557 c.set(Calendar.SECOND, 0); 558 c.set(Calendar.MILLISECOND, 0); 559 560 return c.getTime(); 561 } 562 563 581 public static Date getEvenHourDateBefore(Date date) { 582 if (date == null) { 583 date = new Date (); 584 } 585 586 Calendar c = Calendar.getInstance(); 587 c.setTime(date); 588 589 c.set(Calendar.MINUTE, 0); 590 c.set(Calendar.SECOND, 0); 591 c.set(Calendar.MILLISECOND, 0); 592 593 return c.getTime(); 594 } 595 596 615 public static Date getEvenMinuteDate(Date date) { 616 if (date == null) { 617 date = new Date (); 618 } 619 620 Calendar c = Calendar.getInstance(); 621 c.setTime(date); 622 c.setLenient(true); 623 624 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 625 c.set(Calendar.SECOND, 0); 626 c.set(Calendar.MILLISECOND, 0); 627 628 return c.getTime(); 629 } 630 631 649 public static Date getEvenMinuteDateBefore(Date date) { 650 if (date == null) { 651 date = new Date (); 652 } 653 654 Calendar c = Calendar.getInstance(); 655 c.setTime(date); 656 657 c.set(Calendar.SECOND, 0); 658 c.set(Calendar.MILLISECOND, 0); 659 660 return c.getTime(); 661 } 662 663 676 public static Date getEvenSecondDate(Date date) { 677 if (date == null) { 678 date = new Date (); 679 } 680 681 Calendar c = Calendar.getInstance(); 682 c.setTime(date); 683 c.setLenient(true); 684 685 c.set(Calendar.SECOND, c.get(Calendar.SECOND) + 1); 686 c.set(Calendar.MILLISECOND, 0); 687 688 return c.getTime(); 689 } 690 691 709 public static Date getEvenSecondDateBefore(Date date) { 710 if (date == null) { 711 date = new Date (); 712 } 713 714 Calendar c = Calendar.getInstance(); 715 c.setTime(date); 716 717 c.set(Calendar.MILLISECOND, 0); 718 719 return c.getTime(); 720 } 721 722 817 public static Date getNextGivenMinuteDate(Date date, int minuteBase) { 818 if (minuteBase < 0 || minuteBase > 59) { 819 throw new IllegalArgumentException ( 820 "minuteBase must be >=0 and <= 59"); 821 } 822 823 if (date == null) { 824 date = new Date (); 825 } 826 827 Calendar c = Calendar.getInstance(); 828 c.setTime(date); 829 c.setLenient(true); 830 831 if (minuteBase == 0) { 832 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 833 c.set(Calendar.MINUTE, 0); 834 c.set(Calendar.SECOND, 0); 835 c.set(Calendar.MILLISECOND, 0); 836 837 return c.getTime(); 838 } 839 840 int minute = c.get(Calendar.MINUTE); 841 842 int arItr = minute / minuteBase; 843 844 int nextMinuteOccurance = minuteBase * (arItr + 1); 845 846 if (nextMinuteOccurance < 60) { 847 c.set(Calendar.MINUTE, nextMinuteOccurance); 848 c.set(Calendar.SECOND, 0); 849 c.set(Calendar.MILLISECOND, 0); 850 851 return c.getTime(); 852 } else { 853 c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); 854 c.set(Calendar.MINUTE, 0); 855 c.set(Calendar.SECOND, 0); 856 c.set(Calendar.MILLISECOND, 0); 857 858 return c.getTime(); 859 } 860 } 861 862 881 public static Date getNextGivenSecondDate(Date date, int secondBase) { 882 if (secondBase < 0 || secondBase > 59) { 883 throw new IllegalArgumentException ( 884 "secondBase must be >=0 and <= 59"); 885 } 886 887 if (date == null) { 888 date = new Date (); 889 } 890 891 Calendar c = Calendar.getInstance(); 892 c.setTime(date); 893 c.setLenient(true); 894 895 if (secondBase == 0) { 896 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 897 c.set(Calendar.SECOND, 0); 898 c.set(Calendar.MILLISECOND, 0); 899 900 return c.getTime(); 901 } 902 903 int second = c.get(Calendar.SECOND); 904 905 int arItr = second / secondBase; 906 907 int nextSecondOccurance = secondBase * (arItr + 1); 908 909 if (nextSecondOccurance < 60) { 910 c.set(Calendar.SECOND, nextSecondOccurance); 911 c.set(Calendar.MILLISECOND, 0); 912 913 return c.getTime(); 914 } else { 915 c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); 916 c.set(Calendar.SECOND, 0); 917 c.set(Calendar.MILLISECOND, 0); 918 919 return c.getTime(); 920 } 921 } 922 923 939 public static Date getDateOf(int second, int minute, int hour) { 940 validateSecond(second); 941 validateMinute(minute); 942 validateHour(hour); 943 944 Date date = new Date (); 945 946 Calendar c = Calendar.getInstance(); 947 c.setTime(date); 948 c.setLenient(true); 949 950 c.set(Calendar.HOUR_OF_DAY, hour); 951 c.set(Calendar.MINUTE, minute); 952 c.set(Calendar.SECOND, second); 953 c.set(Calendar.MILLISECOND, 0); 954 955 return c.getTime(); 956 } 957 958 978 public static Date getDateOf(int second, int minute, int hour, 979 int dayOfMonth, int month) { 980 validateSecond(second); 981 validateMinute(minute); 982 validateHour(hour); 983 validateDayOfMonth(dayOfMonth); 984 validateMonth(month); 985 986 Date date = new Date (); 987 988 Calendar c = Calendar.getInstance(); 989 c.setTime(date); 990 991 c.set(Calendar.MONTH, month - 1); 992 c.set(Calendar.DAY_OF_MONTH, dayOfMonth); 993 c.set(Calendar.HOUR_OF_DAY, hour); 994 c.set(Calendar.MINUTE, minute); 995 c.set(Calendar.SECOND, second); 996 c.set(Calendar.MILLISECOND, 0); 997 998 return c.getTime(); 999 } 1000 1001 1023 public static Date getDateOf(int second, int minute, int hour, 1024 int dayOfMonth, int month, int year) { 1025 validateSecond(second); 1026 validateMinute(minute); 1027 validateHour(hour); 1028 validateDayOfMonth(dayOfMonth); 1029 validateMonth(month); 1030 validateYear(year); 1031 1032 Date date = new Date (); 1033 1034 Calendar c = Calendar.getInstance(); 1035 c.setTime(date); 1036 1037 c.set(Calendar.YEAR, year); 1038 c.set(Calendar.MONTH, month - 1); 1039 c.set(Calendar.DAY_OF_MONTH, dayOfMonth); 1040 c.set(Calendar.HOUR_OF_DAY, hour); 1041 c.set(Calendar.MINUTE, minute); 1042 c.set(Calendar.SECOND, second); 1043 c.set(Calendar.MILLISECOND, 0); 1044 1045 return c.getTime(); 1046 } 1047 1048 1064 public static List computeFireTimes(Trigger trigg, org.quartz.Calendar cal, 1065 int numTimes) { 1066 LinkedList lst = new LinkedList (); 1067 1068 Trigger t = (Trigger) trigg.clone(); 1069 1070 if (t.getNextFireTime() == null) { 1071 t.computeFirstFireTime(cal); 1072 } 1073 1074 for (int i = 0; i < numTimes; i++) { 1075 Date d = t.getNextFireTime(); 1076 if (d != null) { 1077 lst.add(d); 1078 t.triggered(cal); 1079 } else { 1080 break; 1081 } 1082 } 1083 1084 return java.util.Collections.unmodifiableList(lst); 1085 } 1086 1087 1107 public static List computeFireTimesBetween(Trigger trigg, 1108 org.quartz.Calendar cal, Date from, Date to) { 1109 LinkedList lst = new LinkedList (); 1110 1111 Trigger t = (Trigger) trigg.clone(); 1112 1113 if (t.getNextFireTime() == null) { 1114 t.computeFirstFireTime(cal); 1115 } 1116 1117 while (true) { 1120 Date d = t.getNextFireTime(); 1121 if (d != null) { 1122 if (d.before(from)) { 1123 t.triggered(cal); 1124 continue; 1125 } 1126 if (d.after(to)) { 1127 break; 1128 } 1129 lst.add(d); 1130 t.triggered(cal); 1131 } else { 1132 break; 1133 } 1134 } 1135 1136 return java.util.Collections.unmodifiableList(lst); 1137 } 1138 1139 1146 public static Date translateTime(Date date, TimeZone src, TimeZone dest) { 1147 1148 Date newDate = new Date (); 1149 1150 int offset = (getOffset(date.getTime(), dest) - getOffset(date.getTime(), src)); 1151 1152 newDate.setTime(date.getTime() - offset); 1153 1154 return newDate; 1155 } 1156 1157 1169 public static int getOffset(long date, TimeZone tz) { 1170 1171 if (tz.inDaylightTime(new Date (date))) { 1172 return tz.getRawOffset() + getDSTSavings(tz); 1173 } 1174 1175 return tz.getRawOffset(); 1176 } 1177 1178 1186 public static int getDSTSavings(TimeZone tz) { 1187 1188 if (tz.useDaylightTime()) { 1189 return 3600000; 1190 } 1191 return 0; 1192 } 1193 1194} 1195 | Popular Tags |