KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quartz > helpers > TriggerUtils


1
2 /*
3  * Copyright 2004-2005 OpenSymphony
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
6  * use this file except in compliance with the License. You may obtain a copy
7  * of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14  * License for the specific language governing permissions and limitations
15  * under the License.
16  *
17  */

18
19 /*
20  * Previously Copyright (c) 2001-2004 James House
21  */

22 package org.quartz.helpers;
23
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.TimeZone JavaDoc;
29
30 import org.quartz.CronTrigger;
31 import org.quartz.Scheduler;
32 import org.quartz.SimpleTrigger;
33 import org.quartz.Trigger;
34
35 /**
36  * <p>
37  * Convenience and utility methods for simplifying the construction and
38  * configuration of <code>{@link Trigger}s</code>.
39  * </p>
40  *
41  * <p>
42  * Please submit suggestions for additional convenience methods to either the
43  * Quartz user forum or the developer's mail list at
44  * <a HREF="http://www.sourceforge.net/projects/quartz">source forge</a>.
45  * </p>
46  *
47  * @see CronTrigger
48  * @see SimpleTrigger
49  *
50  * @deprecated use org.quartz.TriggerUtils instead!
51  *
52  * @author James House
53  */

54 public class TriggerUtils {
55
56     /*
57      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58      *
59      * Constants.
60      *
61      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62      */

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     /**
89      * Private constructor because this is a pure utility class.
90      */

91     private TriggerUtils() {
92     }
93
94     /*
95      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
96      *
97      * Interface.
98      *
99      * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100      */

101
102     private static void validateDayOfWeek(int dayOfWeek) {
103         if (dayOfWeek < SUNDAY || dayOfWeek > SATURDAY) {
104             throw new IllegalArgumentException JavaDoc("Invalid day of week.");
105         }
106     }
107
108     private static void validateHour(int hour) {
109         if (hour < 0 || hour > 23) {
110             throw new IllegalArgumentException JavaDoc(
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 JavaDoc(
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 JavaDoc(
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 JavaDoc("Invalid day of month.");
132         }
133     }
134
135     private static void validateMonth(int month) {
136         if (month < 1 || month > 12) {
137             throw new IllegalArgumentException JavaDoc(
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 JavaDoc(
145                     "Invalid year (must be >= 1970 and <= 2099.");
146         }
147     }
148
149     /**
150      * <p>
151      * Set the given <code>Trigger</code>'s name to the given value, and its
152      * group to the default group (<code>Scheduler.DEFAULT_GROUP</code>).
153      * </p>
154      *
155      * @deprecated use org.quartz.TriggerUtils instead!
156      *
157      */

158     public static void setTriggerIdentity(Trigger trig, String JavaDoc name) {
159         trig.setName(name);
160         trig.setGroup(Scheduler.DEFAULT_GROUP);
161     }
162
163     /**
164      * <p>
165      * Set the given <code>Trigger</code>'s name to the given value, and its
166      * group to the given group.
167      * </p>
168      *
169      * @deprecated use org.quartz.TriggerUtils instead!
170      *
171      */

172     public static void setTriggerIdentity(Trigger trig, String JavaDoc name,
173             String JavaDoc group) {
174         trig.setName(name);
175         trig.setGroup(group);
176     }
177
178     /**
179      * <p>
180      * Make a trigger that will fire every day at the given time.
181      * </p>
182      *
183      * <p>
184      * The generated trigger will still need to have its name, group,
185      * start-time and end-time set.
186      * </p>
187      *
188      * @param hour
189      * the hour (0-23) upon which to fire
190      * @param minute
191      * the minute (0-59) upon which to fire
192      *
193      * @deprecated use org.quartz.TriggerUtils instead!
194      *
195      */

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 JavaDoc ignore) {
205             return null; /* never happens... */
206         }
207
208         return trig;
209     }
210
211     /**
212      * <p>
213      * Make a trigger that will fire every day at the given time.
214      * </p>
215      *
216      * <p>
217      * The generated trigger will still need to have its name, group,
218      * start-time and end-time set.
219      * </p>
220      *
221      * @param dayOfWeek
222      * (1-7) the day of week upon which to fire
223      * @param hour
224      * the hour (0-23) upon which to fire
225      * @param minute
226      * the minute (0-59) upon which to fire
227      *
228      * @see #SUNDAY
229      * @see #MONDAY
230      * @see #TUESDAY
231      * @see #WEDNESDAY
232      * @see #THURSDAY
233      * @see #FRIDAY
234      * @see #SATURDAY
235      *
236      * @deprecated use org.quartz.TriggerUtils instead!
237      *
238      */

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 JavaDoc ignore) {
250             return null; /* never happens... */
251         }
252
253         return trig;
254     }
255
256     /**
257      * <p>
258      * Make a trigger that will fire every day at the given time.
259      * </p>
260      *
261      * <p>
262      * The generated trigger will still need to have its name, group,
263      * start-time and end-time set.
264      * </p>
265      *
266      * <p>
267      * If the day of the month specified does not occur in a given month, a
268      * firing will not occur that month. (i.e. if dayOfMonth is specified as
269      * 31, no firing will occur in the months of the year with fewer than 31
270      * days).
271      * </p>
272      *
273      * @param dayOfMonth
274      * (1-31, or -1) the day of week upon which to fire
275      * @param hour
276      * the hour (0-23) upon which to fire
277      * @param minute
278      * the minute (0-59) upon which to fire
279      *
280      * @deprecated use org.quartz.TriggerUtils instead!
281      *
282      */

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 JavaDoc ignore) {
298             return null; /* never happens... */
299         }
300
301         return trig;
302     }
303
304     /*
305      * <p> Make a trigger that will fire every N days at the given time. </p>
306      *
307      * <p> The generated trigger will still need to have its name, group,
308      * start-time and end-time set. </p>
309      *
310      * @param hour the hour (0-23) upon which to fire @param minute the minute
311      * (0-59) upon which to fire @param interval the number of days between
312      * firings public static Trigger makeDailyTrigger(int interval, int hour,
313      * int minute) {
314      *
315      * SimpleTrigger trig = new SimpleTrigger();
316      *
317      * MILLISECONDS_IN_DAY);
318      * trig.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
319      *
320      * return trig;
321      * }
322      */

323
324     /**
325      * <p>
326      * Make a trigger that will fire every second, indefinitely.
327      * </p>
328      *
329      * <p>
330      * The generated trigger will still need to have its name, group,
331      * start-time and end-time set.
332      * </p>
333      *
334      *
335      * @deprecated use org.quartz.TriggerUtils instead!
336      *
337      */

338     public static Trigger makeSecondlyTrigger() {
339         return makeSecondlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY);
340     }
341
342     /**
343      * <p>
344      * Make a trigger that will fire every N seconds, indefinitely.
345      * </p>
346      *
347      * <p>
348      * The generated trigger will still need to have its name, group,
349      * start-time and end-time set.
350      * </p>
351      *
352      * @param intervalInSeconds
353      * the number of seconds between firings
354      *
355      * @deprecated use org.quartz.TriggerUtils instead!
356      *
357      */

358     public static Trigger makeSecondlyTrigger(int intervalInSeconds) {
359         return makeSecondlyTrigger(intervalInSeconds,
360                 SimpleTrigger.REPEAT_INDEFINITELY);
361     }
362
363     /**
364      * <p>
365      * Make a trigger that will fire every N seconds, with the given number of
366      * repeats.
367      * </p>
368      *
369      * <p>
370      * The generated trigger will still need to have its name, group,
371      * start-time and end-time set.
372      * </p>
373      *
374      * @param intervalInSeconds
375      * the number of seconds between firings
376      * @param repeatCount
377      * the number of times to repeat the firing
378      *
379      * @deprecated use org.quartz.TriggerUtils instead!
380      *
381      */

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     /**
393      * <p>
394      * Make a trigger that will fire every minute, indefinitely.
395      * </p>
396      *
397      * <p>
398      * The generated trigger will still need to have its name, group,
399      * start-time and end-time set.
400      * </p>
401      *
402      *
403      * @deprecated use org.quartz.TriggerUtils instead!
404      *
405      */

406     public static Trigger makeMinutelyTrigger() {
407         return makeMinutelyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY);
408     }
409
410     /**
411      * <p>
412      * Make a trigger that will fire every N minutes, indefinitely.
413      * </p>
414      *
415      * <p>
416      * The generated trigger will still need to have its name, group,
417      * start-time and end-time set.
418      * </p>
419      *
420      * @param intervalInMinutes
421      * the number of minutes between firings
422      *
423      * @deprecated use org.quartz.TriggerUtils instead!
424      *
425      */

426     public static Trigger makeMinutelyTrigger(int intervalInMinutes) {
427         return makeMinutelyTrigger(intervalInMinutes,
428                 SimpleTrigger.REPEAT_INDEFINITELY);
429     }
430
431     /**
432      * <p>
433      * Make a trigger that will fire every N minutes, with the given number of
434      * repeats.
435      * </p>
436      *
437      * <p>
438      * The generated trigger will still need to have its name, group,
439      * start-time and end-time set.
440      * </p>
441      *
442      * @param intervalInMinutes
443      * the number of minutes between firings
444      * @param repeatCount
445      * the number of times to repeat the firing
446      *
447      * @deprecated use org.quartz.TriggerUtils instead!
448      *
449      */

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     /**
461      * <p>
462      * Make a trigger that will fire every hour, indefinitely.
463      * </p>
464      *
465      * <p>
466      * The generated trigger will still need to have its name, group,
467      * start-time and end-time set.
468      * </p>
469      *
470      *
471      * @deprecated use org.quartz.TriggerUtils instead!
472      *
473      */

474     public static Trigger makeHourlyTrigger() {
475         return makeHourlyTrigger(1, SimpleTrigger.REPEAT_INDEFINITELY);
476     }
477
478     /**
479      * <p>
480      * Make a trigger that will fire every N hours, indefinitely.
481      * </p>
482      *
483      * <p>
484      * The generated trigger will still need to have its name, group,
485      * start-time and end-time set.
486      * </p>
487      *
488      * @param intervalInHours
489      * the number of hours between firings
490      *
491      * @deprecated use org.quartz.TriggerUtils instead!
492      *
493      */

494     public static Trigger makeHourlyTrigger(int intervalInHours) {
495         return makeHourlyTrigger(intervalInHours,
496                 SimpleTrigger.REPEAT_INDEFINITELY);
497     }
498
499     /**
500      * <p>
501      * Make a trigger that will fire every N hours, with the given number of
502      * repeats.
503      * </p>
504      *
505      * <p>
506      * The generated trigger will still need to have its name, group,
507      * start-time and end-time set.
508      * </p>
509      *
510      * @param intervalInHours
511      * the number of hours between firings
512      * @param repeatCount
513      * the number of times to repeat the firing
514      *
515      * @deprecated use org.quartz.TriggerUtils instead!
516      *
517      */

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     /**
528      * <p>
529      * Returns a date that is rounded to the next even hour above the given
530      * date.
531      * </p>
532      *
533      * <p>
534      * For example an input date with a time of 08:13:54 would result in a date
535      * with the time of 09:00:00. If the date's time is in the 23rd hour, the
536      * date's 'day' will be promoted, and the time will be set to 00:00:00.
537      * </p>
538      *
539      * @param date
540      * the Date to round, if <code>null</code> the current time will
541      * be used
542      *
543      * @deprecated use org.quartz.TriggerUtils instead!
544      *
545      */

546     public static Date JavaDoc getEvenHourDate(Date JavaDoc date) {
547         if (date == null) {
548             date = new Date JavaDoc();
549         }
550
551         Calendar JavaDoc 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     /**
564      * <p>
565      * Returns a date that is rounded to the previous even hour below the given
566      * date.
567      * </p>
568      *
569      * <p>
570      * For example an input date with a time of 08:13:54 would result in a date
571      * with the time of 08:00:00.
572      * </p>
573      *
574      * @param date
575      * the Date to round, if <code>null</code> the current time will
576      * be used
577      *
578      * @deprecated use org.quartz.TriggerUtils instead!
579      *
580      */

581     public static Date JavaDoc getEvenHourDateBefore(Date JavaDoc date) {
582         if (date == null) {
583             date = new Date JavaDoc();
584         }
585
586         Calendar JavaDoc 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     /**
597      * <p>
598      * Returns a date that is rounded to the next even hour above the given
599      * date.
600      * </p>
601      *
602      * <p>
603      * For example an input date with a time of 08:13:54 would result in a date
604      * with the time of 08:14:00. If the date's time is in the 59th minute,
605      * then the hour (and possibly the day) will be promoted.
606      * </p>
607      *
608      * @param date
609      * the Date to round, if <code>null</code> the current time will
610      * be used
611      *
612      * @deprecated use org.quartz.TriggerUtils instead!
613      *
614      */

615     public static Date JavaDoc getEvenMinuteDate(Date JavaDoc date) {
616         if (date == null) {
617             date = new Date JavaDoc();
618         }
619
620         Calendar JavaDoc 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     /**
632      * <p>
633      * Returns a date that is rounded to the previous even hour below the given
634      * date.
635      * </p>
636      *
637      * <p>
638      * For example an input date with a time of 08:13:54 would result in a date
639      * with the time of 08:13:00.
640      * </p>
641      *
642      * @param date
643      * the Date to round, if <code>null</code> the current time will
644      * be used
645      *
646      * @deprecated use org.quartz.TriggerUtils instead!
647      *
648      */

649     public static Date JavaDoc getEvenMinuteDateBefore(Date JavaDoc date) {
650         if (date == null) {
651             date = new Date JavaDoc();
652         }
653
654         Calendar JavaDoc 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     /**
664      * <p>
665      * Returns a date that is rounded to the next even second above the given
666      * date.
667      * </p>
668      *
669      * @param date
670      * the Date to round, if <code>null</code> the current time will
671      * be used
672      *
673      * @deprecated use org.quartz.TriggerUtils instead!
674      *
675      */

676     public static Date JavaDoc getEvenSecondDate(Date JavaDoc date) {
677         if (date == null) {
678             date = new Date JavaDoc();
679         }
680
681         Calendar JavaDoc 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     /**
692      * <p>
693      * Returns a date that is rounded to the previous even second below the
694      * given date.
695      * </p>
696      *
697      * <p>
698      * For example an input date with a time of 08:13:54.341 would result in a
699      * date with the time of 08:13:00.000.
700      * </p>
701      *
702      * @param date
703      * the Date to round, if <code>null</code> the current time will
704      * be used
705      *
706      * @deprecated use org.quartz.TriggerUtils instead!
707      *
708      */

709     public static Date JavaDoc getEvenSecondDateBefore(Date JavaDoc date) {
710         if (date == null) {
711             date = new Date JavaDoc();
712         }
713
714         Calendar JavaDoc c = Calendar.getInstance();
715         c.setTime(date);
716
717         c.set(Calendar.MILLISECOND, 0);
718
719         return c.getTime();
720     }
721
722     /**
723      * <p>
724      * Returns a date that is rounded to the next even multiple of the given
725      * minute.
726      * </p>
727      *
728      * <p>
729      * For example an input date with a time of 08:13:54, and an input
730      * minute-base of 5 would result in a date with the time of 08:15:00. The
731      * same input date with an input minute-base of 10 would result in a date
732      * with the time of 08:20:00. But a date with the time 08:53:31 and an
733      * input minute-base of 45 would result in 09:00:00, because the even-hour
734      * is the next 'base' for 45-minute intervals.
735      * </p>
736      *
737      * <p>
738      * More examples: <table>
739      * <tr>
740      * <th>Input Time</th>
741      * <th>Minute-Base</th>
742      * <th>Result Time</th>
743      * </tr>
744      * <tr>
745      * <td>11:16:41</td>
746      * <td>20</td>
747      * <td>11:20:00</td>
748      * </tr>
749      * <tr>
750      * <td>11:36:41</td>
751      * <td>20</td>
752      * <td>11:40:00</td>
753      * </tr>
754      * <tr>
755      * <td>11:46:41</td>
756      * <td>20</td>
757      * <td>12:00:00</td>
758      * </tr>
759      * <tr>
760      * <td>11:26:41</td>
761      * <td>30</td>
762      * <td>11:30:00</td>
763      * </tr>
764      * <tr>
765      * <td>11:36:41</td>
766      * <td>30</td>
767      * <td>12:00:00</td>
768      * </tr>
769      * <td>11:16:41</td>
770      * <td>17</td>
771      * <td>11:17:00</td>
772      * </tr>
773      * </tr>
774      * <td>11:17:41</td>
775      * <td>17</td>
776      * <td>11:34:00</td>
777      * </tr>
778      * </tr>
779      * <td>11:52:41</td>
780      * <td>17</td>
781      * <td>12:00:00</td>
782      * </tr>
783      * </tr>
784      * <td>11:52:41</td>
785      * <td>5</td>
786      * <td>11:55:00</td>
787      * </tr>
788      * </tr>
789      * <td>11:57:41</td>
790      * <td>5</td>
791      * <td>12:00:00</td>
792      * </tr>
793      * </tr>
794      * <td>11:17:41</td>
795      * <td>0</td>
796      * <td>12:00:00</td>
797      * </tr>
798      * </tr>
799      * <td>11:17:41</td>
800      * <td>1</td>
801      * <td>11:08:00</td>
802      * </tr>
803      * </table>
804      * </p>
805      *
806      * @param date
807      * the Date to round, if <code>null</code> the current time will
808      * be used
809      * @param minuteBase
810      * the base-minute to set the time on
811      *
812      * @see #getNextGivenSecondDate(Date, int)
813      *
814      * @deprecated use org.quartz.TriggerUtils instead!
815      *
816      */

817     public static Date JavaDoc getNextGivenMinuteDate(Date JavaDoc date, int minuteBase) {
818         if (minuteBase < 0 || minuteBase > 59) {
819             throw new IllegalArgumentException JavaDoc(
820                     "minuteBase must be >=0 and <= 59");
821         }
822
823         if (date == null) {
824             date = new Date JavaDoc();
825         }
826
827         Calendar JavaDoc 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     /**
863      * <p>
864      * Returns a date that is rounded to the next even multiple of the given
865      * minute.
866      * </p>
867      *
868      * <p>
869      * The rules for calculating the second are the same as those for
870      * calculating the minute in the method <code>getNextGivenMinuteDate(..)<code>.</p>
871      * *
872      * @param date the Date to round, if <code>null</code> the current time will
873      * be used
874      * @param secondBase the base-second to set the time on
875      *
876      * @see #getNextGivenMinuteDate(Date, int)
877      *
878      * @deprecated use org.quartz.TriggerUtils instead!
879      *
880      */

881     public static Date JavaDoc getNextGivenSecondDate(Date JavaDoc date, int secondBase) {
882         if (secondBase < 0 || secondBase > 59) {
883             throw new IllegalArgumentException JavaDoc(
884                     "secondBase must be >=0 and <= 59");
885         }
886
887         if (date == null) {
888             date = new Date JavaDoc();
889         }
890
891         Calendar JavaDoc 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     /**
924      * <p>
925      * Get a <code>Date</code> object that represents the given time, on
926      * today's date.
927      * </p>
928      *
929      * @param second
930      * The value (0-59) to give the seconds field of the date
931      * @param minute
932      * The value (0-59) to give the minutes field of the date
933      * @param hour
934      * The value (0-23) to give the hours field of the date
935      *
936      * @deprecated use org.quartz.TriggerUtils instead!
937      *
938      */

939     public static Date JavaDoc getDateOf(int second, int minute, int hour) {
940         validateSecond(second);
941         validateMinute(minute);
942         validateHour(hour);
943
944         Date JavaDoc date = new Date JavaDoc();
945
946         Calendar JavaDoc 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     /**
959      * <p>
960      * Get a <code>Date</code> object that represents the given time, on the
961      * given date.
962      * </p>
963      *
964      * @param second
965      * The value (0-59) to give the seconds field of the date
966      * @param minute
967      * The value (0-59) to give the minutes field of the date
968      * @param hour
969      * The value (0-23) to give the hours field of the date
970      * @param dayOfMonth
971      * The value (1-31) to give the day of month field of the date
972      * @param month
973      * The value (1-12) to give the month field of the date
974      *
975      * @deprecated use org.quartz.TriggerUtils instead!
976      *
977      */

978     public static Date JavaDoc 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 JavaDoc date = new Date JavaDoc();
987
988         Calendar JavaDoc 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    /**
1002     * <p>
1003     * Get a <code>Date</code> object that represents the given time, on the
1004     * given date.
1005     * </p>
1006     *
1007     * @param second
1008     * The value (0-59) to give the seconds field of the date
1009     * @param minute
1010     * The value (0-59) to give the minutes field of the date
1011     * @param hour
1012     * The value (0-23) to give the hours field of the date
1013     * @param dayOfMonth
1014     * The value (1-31) to give the day of month field of the date
1015     * @param month
1016     * The value (1-12) to give the month field of the date
1017     * @param year
1018     * The value (1970-2099) to give the year field of the date
1019     *
1020     * @deprecated use org.quartz.TriggerUtils instead!
1021     *
1022     */

1023    public static Date JavaDoc 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 JavaDoc date = new Date JavaDoc();
1033
1034        Calendar JavaDoc 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    /**
1049     * Returns a list of Dates that are the next fire times of a <code>Trigger</code>.
1050     * The input trigger will be cloned before any work is done, so you need
1051     * not worry about its state being altered by this method.
1052     *
1053     * @param trigg
1054     * The trigger upon which to do the work
1055     * @param cal
1056     * The calendar to apply to the trigger's schedule
1057     * @param numTimes
1058     * The number of next fire times to produce
1059     * @return List of java.util.Date objects
1060     *
1061     * @deprecated use org.quartz.TriggerUtils instead!
1062     *
1063     */

1064    public static List JavaDoc computeFireTimes(Trigger trigg, org.quartz.Calendar cal,
1065            int numTimes) {
1066        LinkedList JavaDoc lst = new LinkedList JavaDoc();
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 JavaDoc 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    /**
1088     * Returns a list of Dates that are the next fire times of a <code>Trigger</code>
1089     * that fall within the given date range. The input trigger will be cloned
1090     * before any work is done, so you need not worry about its state being
1091     * altered by this method.
1092     *
1093     * @param trigg
1094     * The trigger upon which to do the work
1095     * @param cal
1096     * The calendar to apply to the trigger's schedule
1097     * @param from
1098     * The starting date at which to find fire times
1099     * @param to
1100     * The ending date at which to stop finding fire times
1101     * @return List of java.util.Date objects
1102     *
1103     * @deprecated use org.quartz.TriggerUtils instead!
1104     *
1105     *
1106     */

1107    public static List JavaDoc computeFireTimesBetween(Trigger trigg,
1108            org.quartz.Calendar cal, Date JavaDoc from, Date JavaDoc to) {
1109        LinkedList JavaDoc lst = new LinkedList JavaDoc();
1110
1111        Trigger t = (Trigger) trigg.clone();
1112
1113        if (t.getNextFireTime() == null) {
1114            t.computeFirstFireTime(cal);
1115        }
1116
1117        // TODO: this method could be more efficient by using logic specific
1118
// to the type of trigger ...
1119
while (true) {
1120            Date JavaDoc 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    /**
1140     * Translate a date & time from a users timezone to the another
1141     * (probably server) timezone to assist in creating a simple trigger with
1142     * the right date & time.
1143     *
1144     * @deprecated use org.quartz.TriggerUtils instead!
1145     */

1146    public static Date JavaDoc translateTime(Date JavaDoc date, TimeZone JavaDoc src, TimeZone JavaDoc dest) {
1147
1148        Date JavaDoc newDate = new Date JavaDoc();
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    /**
1158     * Gets the offset from UT for the given date in the given timezone,
1159     * taking into account daylight savings.
1160     *
1161     * <p>
1162     * Equivalent of TimeZone.getOffset(date) in JDK 1.4, but Quartz is trying
1163     * to support JDK 1.3.
1164     * </p>
1165     *
1166     *
1167     * @deprecated use org.quartz.TriggerUtils instead!
1168     */

1169    public static int getOffset(long date, TimeZone JavaDoc tz) {
1170        
1171        if (tz.inDaylightTime(new Date JavaDoc(date))) {
1172            return tz.getRawOffset() + getDSTSavings(tz);
1173        }
1174        
1175        return tz.getRawOffset();
1176    }
1177
1178    /**
1179     * <p>
1180     * Equivalent of TimeZone.getDSTSavings() in JDK 1.4, but Quartz is trying
1181     * to support JDK 1.3.
1182     * </p>
1183     *
1184     * @deprecated use org.quartz.TriggerUtils instead!
1185     */

1186    public static int getDSTSavings(TimeZone JavaDoc tz) {
1187        
1188        if (tz.useDaylightTime()) {
1189            return 3600000;
1190        }
1191        return 0;
1192    }
1193    
1194}
1195
Popular Tags