Skip to content

Commit

Permalink
修复cron中在小月时使用“L”的计算问题
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Mar 29, 2024
1 parent bdaa89f commit 74226c4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 🚀Changelog

-------------------------------------------------------------------------------------------------------------
# 5.8.27(2024-03-28)
# 5.8.27(2024-03-29)

### 🐣新特性
* 【extra 】 FreemarkerEngine修改默认版本参数
Expand All @@ -22,6 +22,7 @@
* 【captcha】 修复Graphics2D的资源没释放问题(issue#I98PYN@Gitee)
* 【core 】 修复ClassUtil.getTypeArgument() 获取泛型存在null问题(issue#3516@Github)
* 【core 】 修复图片操作未调用flush导致资源未释放问题(issue#I9C7NA@Gitee)
* 【cron 】 修复cron中在小月时使用“L”的计算问题(pr#1189@Gitee)

-------------------------------------------------------------------------------------------------------------
# 5.8.26(2024-02-10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@
* @author Looly
*/
public class DayOfMonthMatcher extends BoolArrayMatcher {
/**
* 是否是查询最后一天,即“L”
*/
private final boolean isLast;

/**
* 构造
*
* @param intValueList 匹配的日值
*/
public DayOfMonthMatcher(List<Integer> intValueList, boolean isLast) {
public DayOfMonthMatcher(List<Integer> intValueList) {
super(intValueList);
this.isLast = isLast;
}

/**
Expand Down Expand Up @@ -57,7 +51,7 @@ private static boolean isLastDayOfMonth(int value, int month, boolean isLeapYear
}

public boolean isLast() {
return isLast;
return match(31);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import java.time.Year;
import java.util.Calendar;
import java.util.Objects;
import java.util.TimeZone;

/**
Expand Down Expand Up @@ -186,15 +187,19 @@ private int[] nextMatchValuesAfter(int[] values) {
// 周不参与计算
i--;
continue;
} else if (i == Part.DAY_OF_MONTH.ordinal()
}

// pr#1189
if (i == Part.DAY_OF_MONTH.ordinal()
&& matchers[i] instanceof DayOfMonthMatcher
&& ((DayOfMonthMatcher) matchers[i]).isLast()) {
int newMonth = newValues[Part.MONTH.ordinal()];
int newYear = newValues[Part.YEAR.ordinal()];
nextValue = Month.of(newMonth - 1).getLastDay(DateUtil.isLeapYear(newYear));
nextValue = getLastDay(newMonth, newYear);
} else {
nextValue = matchers[i].nextAfter(values[i]);
}

if (nextValue > values[i]) {
// 此部分正常获取新值,结束循环,后续的部分置最小值
newValues[i] = nextValue;
Expand Down Expand Up @@ -224,7 +229,7 @@ private int[] nextMatchValuesAfter(int[] values) {
&& ((DayOfMonthMatcher) matchers[i]).isLast()) {
int newMonth = newValues[Part.MONTH.ordinal()];
int newYear = newValues[Part.YEAR.ordinal()];
nextValue = Month.of(newMonth - 1).getLastDay(DateUtil.isLeapYear(newYear));
nextValue = getLastDay(newMonth, newYear);
} else {
nextValue = matchers[i].nextAfter(values[i] + 1);
}
Expand Down Expand Up @@ -257,7 +262,7 @@ && get(part) instanceof DayOfMonthMatcher
&& ((DayOfMonthMatcher) get(part)).isLast()) {
int newMonth = values[Part.MONTH.ordinal()];
int newYear = values[Part.YEAR.ordinal()];
values[i] = Month.of(newMonth - 1).getLastDay(DateUtil.isLeapYear(newYear));
values[i] = getLastDay(newMonth, newYear);
} else {
values[i] = getMin(part);
}
Expand Down Expand Up @@ -311,4 +316,9 @@ private Calendar setValue(Calendar calendar, Part part, int value) {
//Console.log("Set [{}] as [{}]", part, value);
return calendar;
}

private static int getLastDay(int monthBase1, int year){
return Objects.requireNonNull(Month.of(monthBase1 - 1))
.getLastDay(DateUtil.isLeapYear(year));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,9 @@ public PartParser(Part part) {
*/
public PartMatcher parse(String value) {
// 是否是查询最后一天
boolean isLastDay = false;
if (isMatchAllStr(value)) {
//兼容Quartz的"?"表达式,不会出现互斥情况,与"*"作用相同
return new AlwaysTrueMatcher();
} else if ("L".equalsIgnoreCase(value)) {
isLastDay = true;
}

final List<Integer> values = parseArray(value);
Expand All @@ -80,7 +77,7 @@ public PartMatcher parse(String value) {

switch (this.part) {
case DAY_OF_MONTH:
return new DayOfMonthMatcher(values, isLastDay);
return new DayOfMonthMatcher(values);
case YEAR:
return new YearValueMatcher(values);
default:
Expand Down

0 comments on commit 74226c4

Please sign in to comment.