| | |
| | | return calculateWeeklyFirstExecution(task.getFrequencyDetail()); |
| | | } else if ("MONTHLY".equals(frequencyType)) { |
| | | return calculateMonthlyFirstExecution(task.getFrequencyDetail()); |
| | | } else if ("YEARLY".equals(frequencyType)) { |
| | | return calculateYearlyFirstExecution(task.getFrequencyDetail()); |
| | | } else if ("QUARTERLY".equals(frequencyType)) { |
| | | return calculateCustomFirstExecution(task.getFrequencyDetail()); |
| | | } else { |
| | |
| | | return targetDateTime; |
| | | } |
| | | |
| | | private LocalDateTime calculateYearlyFirstExecution(String frequencyDetail) { |
| | | String[] parts = frequencyDetail.split(","); |
| | | if (parts.length != 3) { |
| | | throw new IllegalArgumentException("参数格式错误,应为 03,15,17:00"); |
| | | } |
| | | |
| | | int month = Integer.parseInt(parts[0].trim()); |
| | | int dayOfMonth = Integer.parseInt(parts[1].trim()); |
| | | LocalTime targetTime = LocalTime.parse(parts[2].trim(), DateTimeFormatter.ofPattern("HH:mm")); |
| | | |
| | | LocalDateTime now = LocalDateTime.now(); |
| | | YearMonth currentYearMonth = YearMonth.of(now.getYear(), month); |
| | | int adjustedDay = Math.min(dayOfMonth, currentYearMonth.lengthOfMonth()); |
| | | LocalDateTime targetDateTime = LocalDateTime.of(now.getYear(), month, adjustedDay, targetTime.getHour(), targetTime.getMinute()); |
| | | if (!targetDateTime.isAfter(now)) { |
| | | YearMonth nextYearMonth = YearMonth.of(now.getYear() + 1, month); |
| | | adjustedDay = Math.min(dayOfMonth, nextYearMonth.lengthOfMonth()); |
| | | targetDateTime = LocalDateTime.of(now.getYear() + 1, month, adjustedDay, targetTime.getHour(), targetTime.getMinute()); |
| | | } |
| | | return targetDateTime; |
| | | } |
| | | |
| | | private LocalDateTime calculateCustomFirstExecution(String frequencyDetail) { |
| | | return null; |
| | | } |
| | |
| | | return calculateMonthlyNextTime(frequencyDetail, currentTime); |
| | | case "QUARTERLY": |
| | | return calculateQuarterlyNextTime(frequencyDetail, currentTime); |
| | | case "YEARLY": |
| | | return calculateYearlyNextTime(frequencyDetail, currentTime); |
| | | default: |
| | | throw new IllegalArgumentException("不支持的频率类型: " + frequencyType); |
| | | } |
| | |
| | | ); |
| | | } |
| | | |
| | | private LocalDateTime calculateYearlyNextTime(String detail, LocalDateTime current) { |
| | | String[] parts = detail.split(","); |
| | | int month = Integer.parseInt(parts[0]); |
| | | int dayOfMonth = Integer.parseInt(parts[1]); |
| | | LocalTime time = LocalTime.parse(parts[2]); |
| | | |
| | | YearMonth targetYearMonth = YearMonth.of(current.getYear(), month); |
| | | int adjustedDay = Math.min(dayOfMonth, targetYearMonth.lengthOfMonth()); |
| | | LocalDateTime target = LocalDateTime.of(current.getYear(), month, adjustedDay, time.getHour(), time.getMinute()); |
| | | if (!target.isAfter(current)) { |
| | | targetYearMonth = YearMonth.of(current.getYear() + 1, month); |
| | | adjustedDay = Math.min(dayOfMonth, targetYearMonth.lengthOfMonth()); |
| | | target = LocalDateTime.of(current.getYear() + 1, month, adjustedDay, time.getHour(), time.getMinute()); |
| | | } |
| | | return target; |
| | | } |
| | | |
| | | private Set<DayOfWeek> parseDayOfWeeks(String dayOfWeekStr) { |
| | | Set<DayOfWeek> days = new HashSet<>(); |
| | | String[] dayStrs = dayOfWeekStr.split("\\|"); |