📄 triggerutils.java
字号:
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 hour above the given * date. * </p> * * <p> * For example an input date with a time of 08:13:54 would result in a date * with the time of 08:14:00. If the date's time is in the 59th minute, * then the hour (and possibly the day) will be promoted. * </p> * * @param date * the Date to round, if <code>null</code> the current time will * be used */ public static Date getEvenMinuteDate(Date date) { if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.setLenient(true); c.set(Calendar.MINUTE, c.get(Calendar.MINUTE) + 1); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * <p> * Returns a date that is rounded to the previous even hour below the given * date. * </p> * * <p> * For example an input date with a time of 08:13:54 would result in a date * with the time of 08:13:00. * </p> * * @param date * the Date to round, if <code>null</code> the current time will * be used */ public static Date getEvenMinuteDateBefore(Date date) { if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.set(Calendar.SECOND, 0); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * <p> * Returns a date that is rounded to the next even second above the given * date. * </p> * * @param date * the Date to round, if <code>null</code> the current time will * be used */ public static Date getEvenSecondDate(Date date) { if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); c.setLenient(true); c.set(Calendar.SECOND, c.get(Calendar.SECOND) + 1); c.set(Calendar.MILLISECOND, 0); return c.getTime(); } /** * <p> * Returns a date that is rounded to the previous even second below the * given date. * </p> * * <p> * For example an input date with a time of 08:13:54.341 would result in a * date with the time of 08:13:00.000. * </p> * * @param date * the Date to round, if <code>null</code> the current time will * be used */ public static Date getEvenSecondDateBefore(Date date) { if (date == null) date = new Date(); Calendar c = Calendar.getInstance(); c.setTime(date); 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> * For example an input date with a time of 08:13:54, and an input * minute-base of 5 would result in a date with the time of 08:15:00. The * same input date with an input minute-base of 10 would result in a date * with the time of 08:20:00. But a date with the time 08:53:31 and an * 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 * * @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 * * @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 */ 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 */ 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 */ 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. * * @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.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); }// NOT JDK 1.3 compatable. // 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./* public static Date translateTime(Date date, TimeZone src, TimeZone dest) { Date newDate = new Date(); int offset = (dest.getOffset(date.getTime()) - src.getOffset(date .getTime())); newDate.setTime(date.getTime() - offset); return newDate; }*/ }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -