!1426 fix issue IDFVKG, 在 DataBetween 类的构造函数中做 defensive copy

Merge pull request !1426 from shad0wm00n/v5-dev-1225-4
This commit is contained in:
Looly
2025-12-25 16:04:51 +00:00
committed by Gitee
2 changed files with 21 additions and 4 deletions

View File

@@ -74,13 +74,17 @@ public class DateBetween implements Serializable {
Assert.notNull(begin, "Begin date is null !");
Assert.notNull(end, "End date is null !");
// defensive copy
Date b = new Date(begin.getTime());
Date e = new Date(end.getTime());
if (isAbs && begin.after(end)) {
// 间隔只为正数的情况下,如果开始日期晚于结束日期,置换之
this.begin = end;
this.end = begin;
this.begin = e;
this.end = b;
} else {
this.begin = begin;
this.end = end;
this.begin = b;
this.end = e;
}
}

View File

@@ -96,4 +96,17 @@ public class DateBetweenTest {
long result = DateUtil.betweenYear(sdate, edate, false);
assertEquals(0, result);
}
@Test
public void issueIDFVKGTest() {
Date b = new Date(1609459200000L); // 2021-01-01 00:00:00
Date e = new Date(1609545600000L); // 2021-01-02 00:00:00
DateBetween db = new DateBetween(b, e);
// 修改原始 date
b.setTime(0L); // 1970-01-01
// 期望 DateBetween 不受影响,间隔仍为 1 天
assertEquals(1, db.between(DateUnit.DAY));
}
}