mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-05-09 23:51:34 +08:00
修复DateUtil.rangeToList中step小于等于0时无限循环问题
This commit is contained in:
parent
e0a554326f
commit
9c6225dafa
@ -64,6 +64,10 @@ public class DateRange extends Range<DateTime> {
|
|||||||
*/
|
*/
|
||||||
public DateRange(final Date start, final Date end, final DateField unit, final int step, final boolean isIncludeStart, final boolean isIncludeEnd) {
|
public DateRange(final Date start, final Date end, final DateField unit, final int step, final boolean isIncludeStart, final boolean isIncludeEnd) {
|
||||||
super(DateUtil.date(start), DateUtil.date(end), (current, end1, index) -> {
|
super(DateUtil.date(start), DateUtil.date(end), (current, end1, index) -> {
|
||||||
|
if(step <= 0){
|
||||||
|
// issue#3783
|
||||||
|
return null;
|
||||||
|
}
|
||||||
final DateTime dt = DateUtil.date(start).offsetNew(unit, (index + 1) * step);
|
final DateTime dt = DateUtil.date(start).offsetNew(unit, (index + 1) * step);
|
||||||
if (dt.isAfter(end1)) {
|
if (dt.isAfter(end1)) {
|
||||||
return null;
|
return null;
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
package org.dromara.hutool.core.date;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class DateRangeTest {
|
||||||
|
@Test
|
||||||
|
void issue3783Test() {
|
||||||
|
final Date start = DateUtil.parse("2024-01-01");
|
||||||
|
final Date end = DateUtil.parse("2024-02-01");
|
||||||
|
final List<DateTime> dateTimes = DateUtil.rangeToList(start, end, DateField.DAY_OF_MONTH, 0);
|
||||||
|
Assertions.assertEquals(1, dateTimes.size());
|
||||||
|
Assertions.assertEquals("2024-01-01 00:00:00", dateTimes.get(0).toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void issue3783Test2() {
|
||||||
|
final Date start = DateUtil.parse("2024-01-01");
|
||||||
|
final Date end = DateUtil.parse("2024-02-01");
|
||||||
|
final List<DateTime> dateTimes = DateUtil.rangeToList(start, end, DateField.DAY_OF_MONTH, -2);
|
||||||
|
Assertions.assertEquals(1, dateTimes.size());
|
||||||
|
Assertions.assertEquals("2024-01-01 00:00:00", dateTimes.get(0).toString());
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user