?? triggerutils.java
字號:
* input minute-base of 45 would result in 09:00:00, because the even-hour * is the next 'base' for 45-minute intervals. * </p> * * <p> * More examples: <table> * <tr> * <th>Input Time</th> * <th>Minute-Base</th> * <th>Result Time</th> * </tr> * <tr> * <td>11:16:41</td> * <td>20</td> * <td>11:20:00</td> * </tr> * <tr> * <td>11:36:41</td> * <td>20</td> * <td>11:40:00</td> * </tr> * <tr> * <td>11:46:41</td> * <td>20</td> * <td>12:00:00</td> * </tr> * <tr> * <td>11:26:41</td> * <td>30</td> * <td>11:30:00</td> * </tr> * <tr> * <td>11:36:41</td> * <td>30</td> * <td>12:00:00</td> * </tr> * <td>11:16:41</td> * <td>17</td> * <td>11:17:00</td> * </tr> * </tr> * <td>11:17:41</td> * <td>17</td> * <td>11:34:00</td> * </tr> * </tr> * <td>11:52:41</td> * <td>17</td> * <td>12:00:00</td> * </tr> * </tr> * <td>11:52:41</td> * <td>5</td> * <td>11:55:00</td> * </tr> * </tr> * <td>11:57:41</td> * <td>5</td> * <td>12:00:00</td> * </tr> * </tr> * <td>11:17:41</td> * <td>0</td> * <td>12:00:00</td> * </tr> * </tr> * <td>11:17:41</td> * <td>1</td> * <td>11:08:00</td> * </tr> * </table> * </p> * * @param date * the Date to round, if <code>null</code> the current time will * be used * @param minuteBase * the base-minute to set the time on * @return the new rounded date * * @see #getNextGivenSecondDate(Date, int) */ public static Date getNextGivenMinuteDate(Date date, int minuteBase) { if (minuteBase < 0 || minuteBase > 59) throw new IllegalArgumentException( "minuteBase must be >=0 and <= 59"); if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.setLenient(true); if (minuteBase == 0) { c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } int minute = c.get(Calendar.MINUTE); int arItr = minute / minuteBase; int nextMinuteOccurance = minuteBase * (arItr + 1); if (nextMinuteOccurance < 60) { c.set(Calendar.MINUTE, nextMinuteOccurance); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } else { c.set(Calendar.HOUR_OF_DAY, c.get(Calendar.HOUR_OF_DAY) + 1); c.set(Calendar.MINUTE, 0); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } } /** * <p> * Returns a date that is rounded to the next even multiple of the given * minute. * </p> * * <p> * The rules for calculating the second are the same as those for * calculating the minute in the method * <code>getNextGivenMinuteDate(..)<code>. * </p> * * @param date the Date to round, if <code>null</code> the current time will * be used * @param secondBase the base-second to set the time on * @return the new rounded date * * @see #getNextGivenMinuteDate(Date, int) */ public static Date getNextGivenSecondDate(Date date, int secondBase) { if (secondBase < 0 || secondBase > 59) throw new IllegalArgumentException( "secondBase must be >=0 and <= 59"); if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.setLenient(true); if (secondBase == 0) { c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } int second = c.get(Calendar.SECOND); int arItr = second / secondBase; int nextSecondOccurance = secondBase * (arItr + 1); if (nextSecondOccurance < 60) { c.set(Calendar.SECOND, nextSecondOccurance); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } else { c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } } /** * <p> * Get a <code>Date</code> object that represents the given time, on * today's date. * </p> * * @param second * The value (0-59) to give the seconds field of the date * @param minute * The value (0-59) to give the minutes field of the date * @param hour * The value (0-23) to give the hours field of the date * @return the new date */ public static Date getDateOf(int second, int minute, int hour) { validateSecond(second); validateMinute(minute); validateHour(hour); Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.setLenient(true); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * <p> * Get a <code>Date</code> object that represents the given time, on the * given date. * </p> * * @param second * The value (0-59) to give the seconds field of the date * @param minute * The value (0-59) to give the minutes field of the date * @param hour * The value (0-23) to give the hours field of the date * @param dayOfMonth * The value (1-31) to give the day of month field of the date * @param month * The value (1-12) to give the month field of the date * @return the new date */ public static Date getDateOf(int second, int minute, int hour, int dayOfMonth, int month) { validateSecond(second); validateMinute(minute); validateHour(hour); validateDayOfMonth(dayOfMonth); validateMonth(month); Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, dayOfMonth); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * <p> * Get a <code>Date</code> object that represents the given time, on the * given date. * </p> * * @param second * The value (0-59) to give the seconds field of the date * @param minute * The value (0-59) to give the minutes field of the date * @param hour * The value (0-23) to give the hours field of the date * @param dayOfMonth * The value (1-31) to give the day of month field of the date * @param month * The value (1-12) to give the month field of the date * @param year * The value (1970-2099) to give the year field of the date * @return the new date */ public static Date getDateOf(int second, int minute, int hour, int dayOfMonth, int month, int year) { validateSecond(second); validateMinute(minute); validateHour(hour); validateDayOfMonth(dayOfMonth); validateMonth(month); validateYear(year); Date date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.YEAR, year); c.set(Calendar.MONTH, month - 1); c.set(Calendar.DAY_OF_MONTH, dayOfMonth); c.set(Calendar.HOUR_OF_DAY, hour); c.set(Calendar.MINUTE, minute); c.set(Calendar.SECOND, second); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * Returns a list of Dates that are the next fire times of a * <code>Trigger</code>. * The input trigger will be cloned before any work is done, so you need * not worry about its state being altered by this method. * * @param trigg * The trigger upon which to do the work * @param cal * The calendar to apply to the trigger's schedule * @param numTimes * The number of next fire times to produce * @return List of java.util.Date objects */ public static List computeFireTimes(Trigger trigg, org.quartz.Calendar cal, int numTimes) { LinkedList lst = new LinkedList(); Trigger t = (Trigger) trigg.clone(); if (t.getNextFireTime() == null) { t.computeFirstFireTime(cal); } for (int i = 0; i < numTimes; i++) { Date d = t.getNextFireTime(); if (d != null) { lst.add(d); t.triggered(cal); } else break; } return java.util.Collections.unmodifiableList(lst); } /** * Returns a list of Dates that are the next fire times of a * <code>Trigger</code> * that fall within the given date range. The input trigger will be cloned * before any work is done, so you need not worry about its state being * altered by this method. * * <p> * NOTE: if this is a trigger that has previously fired within the given * date range, then firings which have already occured will not be listed * in the output List. * </p> * * @param trigg * The trigger upon which to do the work * @param cal * The calendar to apply to the trigger's schedule * @param from * The starting date at which to find fire times * @param to * The ending date at which to stop finding fire times * @return List of java.util.Date objects */ public static List computeFireTimesBetween(Trigger trigg, org.quartz.Calendar cal, Date from, Date to) { LinkedList lst = new LinkedList(); Trigger t = (Trigger) trigg.clone(); if (t.getNextFireTime() == null) { t.setStartTime(from); t.setEndTime(to); t.computeFirstFireTime(cal); } // TODO: this method could be more efficient by using logic specific // to the type of trigger ... while (true) { Date d = t.getNextFireTime(); if (d != null) { if (d.before(from)) { t.triggered(cal); continue; } if (d.after(to)) break; lst.add(d); t.triggered(cal); } else break; } return java.util.Collections.unmodifiableList(lst); } /** * Translate a date & time from a users timezone to the another * (probably server) timezone to assist in creating a simple trigger with * the right date & time. * * @param date the date to translate * @param src the original time-zone * @param dest the destination time-zone * @return the translated date */ public static Date translateTime(Date date, TimeZone src, TimeZone dest) { Date newDate = new Date(); int offset = ( getOffset(date.getTime(), dest) - getOffset(date.getTime(), src) ); newDate.setTime(date.getTime() - offset); return newDate; } /** * Gets the offset from UT for the given date in the given timezone, * taking into account daylight savings. * * <p> * Equivalent of TimeZone.getOffset(date) in JDK 1.4, but Quartz is trying * to support JDK 1.3. * </p> * * @param date the date (in milliseconds) that is the base for the offset * @param tz the time-zone to calculate to offset to * @return the offset */ public static int getOffset(long date, TimeZone tz) { if (tz.inDaylightTime(new Date(date))) { return tz.getRawOffset() + getDSTSavings(tz); } return tz.getRawOffset(); } /** * <p> * Equivalent of TimeZone.getDSTSavings() in JDK 1.4, but Quartz is trying * to support JDK 1.3. * </p> * * @param tz the target time-zone * @return the amount of saving time in milliseconds */ public static int getDSTSavings(TimeZone tz) { if (tz.useDaylightTime()) { return 3600000; } return 0; }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -