@Test public void testGetPreCycleDate() { Date now = new Date(); System.out.println("now: " + now); Map.Entry<Date, Date> minMap = CrontabUtil.getPreCycleDate(now, ScheduleType.MINUTE); System.out.println("min level is " + minMap.getKey() + " - " + minMap.getValue()); Map.Entry<Date, Date> hourMap = CrontabUtil.getPreCycleDate(now, ScheduleType.HOUR); System.out.println("hour level is " + hourMap.getKey() + " - " + hourMap.getValue()); Map.Entry<Date, Date> dayMap = CrontabUtil.getPreCycleDate(now, ScheduleType.DAY); System.out.println("day level is " + dayMap.getKey() + " - " + dayMap.getValue()); Map.Entry<Date, Date> weekMap = CrontabUtil.getPreCycleDate(now, ScheduleType.WEEK); System.out.println("week level is " + weekMap.getKey() + " - " + weekMap.getValue()); Map.Entry<Date, Date> monthMap = CrontabUtil.getPreCycleDate(now, ScheduleType.MONTH); System.out.println("month level is " + monthMap.getKey() + " - " + monthMap.getValue()); }
public static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType) { Calendar scheduleStartTime = Calendar.getInstance(); scheduleStartTime.setTime(scheduledFireTime); Calendar scheduleEndTime = Calendar.getInstance(); scheduleEndTime.setTime(scheduledFireTime); switch (scheduleType) { case MINUTE: scheduleStartTime.add(Calendar.MINUTE, -1); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case HOUR: scheduleStartTime.add(Calendar.HOUR_OF_DAY, -1); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case DAY: scheduleStartTime.add(Calendar.DAY_OF_MONTH, -1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case WEEK: scheduleStartTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleEndTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleStartTime.add(Calendar.WEEK_OF_YEAR, -1); scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case MONTH: scheduleStartTime.add(Calendar.MONTH, -1); scheduleStartTime.set(Calendar.DAY_OF_MONTH, 1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_MONTH, scheduleEndTime.getActualMaximum(Calendar.DAY_OF_MONTH)); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; default: } return new AbstractMap.SimpleImmutableEntry<>(scheduleStartTime.getTime(), scheduleEndTime.getTime()); }
CrontabUtil { public static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType) { Calendar scheduleStartTime = Calendar.getInstance(); scheduleStartTime.setTime(scheduledFireTime); Calendar scheduleEndTime = Calendar.getInstance(); scheduleEndTime.setTime(scheduledFireTime); switch (scheduleType) { case MINUTE: scheduleStartTime.add(Calendar.MINUTE, -1); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case HOUR: scheduleStartTime.add(Calendar.HOUR_OF_DAY, -1); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case DAY: scheduleStartTime.add(Calendar.DAY_OF_MONTH, -1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case WEEK: scheduleStartTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleEndTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleStartTime.add(Calendar.WEEK_OF_YEAR, -1); scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case MONTH: scheduleStartTime.add(Calendar.MONTH, -1); scheduleStartTime.set(Calendar.DAY_OF_MONTH, 1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_MONTH, scheduleEndTime.getActualMaximum(Calendar.DAY_OF_MONTH)); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; default: } return new AbstractMap.SimpleImmutableEntry<>(scheduleStartTime.getTime(), scheduleEndTime.getTime()); } }
CrontabUtil { public static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType) { Calendar scheduleStartTime = Calendar.getInstance(); scheduleStartTime.setTime(scheduledFireTime); Calendar scheduleEndTime = Calendar.getInstance(); scheduleEndTime.setTime(scheduledFireTime); switch (scheduleType) { case MINUTE: scheduleStartTime.add(Calendar.MINUTE, -1); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case HOUR: scheduleStartTime.add(Calendar.HOUR_OF_DAY, -1); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case DAY: scheduleStartTime.add(Calendar.DAY_OF_MONTH, -1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case WEEK: scheduleStartTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleEndTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleStartTime.add(Calendar.WEEK_OF_YEAR, -1); scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case MONTH: scheduleStartTime.add(Calendar.MONTH, -1); scheduleStartTime.set(Calendar.DAY_OF_MONTH, 1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_MONTH, scheduleEndTime.getActualMaximum(Calendar.DAY_OF_MONTH)); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; default: } return new AbstractMap.SimpleImmutableEntry<>(scheduleStartTime.getTime(), scheduleEndTime.getTime()); }  }
CrontabUtil { public static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType) { Calendar scheduleStartTime = Calendar.getInstance(); scheduleStartTime.setTime(scheduledFireTime); Calendar scheduleEndTime = Calendar.getInstance(); scheduleEndTime.setTime(scheduledFireTime); switch (scheduleType) { case MINUTE: scheduleStartTime.add(Calendar.MINUTE, -1); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case HOUR: scheduleStartTime.add(Calendar.HOUR_OF_DAY, -1); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case DAY: scheduleStartTime.add(Calendar.DAY_OF_MONTH, -1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case WEEK: scheduleStartTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleEndTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleStartTime.add(Calendar.WEEK_OF_YEAR, -1); scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case MONTH: scheduleStartTime.add(Calendar.MONTH, -1); scheduleStartTime.set(Calendar.DAY_OF_MONTH, 1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_MONTH, scheduleEndTime.getActualMaximum(Calendar.DAY_OF_MONTH)); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; default: } return new AbstractMap.SimpleImmutableEntry<>(scheduleStartTime.getTime(), scheduleEndTime.getTime()); }  static Cron parseCron(String crontab); static CronExpression parseCronExp(String crontab); static ScheduleType getCycle(Cron cron); static ScheduleType getCycle(String crontab); static List<Date> getCycleFireDate(Date startTime, Date endTime, CronExpression cronExpression); static ZonedDateTime parseDateToZdt(Date date); static Date parseZdtToDate(ZonedDateTime zdt); static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType); }
CrontabUtil { public static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType) { Calendar scheduleStartTime = Calendar.getInstance(); scheduleStartTime.setTime(scheduledFireTime); Calendar scheduleEndTime = Calendar.getInstance(); scheduleEndTime.setTime(scheduledFireTime); switch (scheduleType) { case MINUTE: scheduleStartTime.add(Calendar.MINUTE, -1); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case HOUR: scheduleStartTime.add(Calendar.HOUR_OF_DAY, -1); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case DAY: scheduleStartTime.add(Calendar.DAY_OF_MONTH, -1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case WEEK: scheduleStartTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleEndTime.setFirstDayOfWeek(Calendar.MONDAY); scheduleStartTime.add(Calendar.WEEK_OF_YEAR, -1); scheduleStartTime.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; case MONTH: scheduleStartTime.add(Calendar.MONTH, -1); scheduleStartTime.set(Calendar.DAY_OF_MONTH, 1); scheduleStartTime.set(Calendar.HOUR_OF_DAY, 0); scheduleStartTime.set(Calendar.MINUTE, 0); scheduleStartTime.set(Calendar.SECOND, 0); scheduleEndTime.setTime(scheduleStartTime.getTime()); scheduleEndTime.set(Calendar.DAY_OF_MONTH, scheduleEndTime.getActualMaximum(Calendar.DAY_OF_MONTH)); scheduleEndTime.set(Calendar.HOUR_OF_DAY, 0); scheduleEndTime.set(Calendar.MINUTE, 0); scheduleEndTime.set(Calendar.SECOND, 0); break; default: } return new AbstractMap.SimpleImmutableEntry<>(scheduleStartTime.getTime(), scheduleEndTime.getTime()); }  static Cron parseCron(String crontab); static CronExpression parseCronExp(String crontab); static ScheduleType getCycle(Cron cron); static ScheduleType getCycle(String crontab); static List<Date> getCycleFireDate(Date startTime, Date endTime, CronExpression cronExpression); static ZonedDateTime parseDateToZdt(Date date); static Date parseZdtToDate(ZonedDateTime zdt); static Map.Entry<Date, Date> getPreCycleDate(Date scheduledFireTime, ScheduleType scheduleType);  }