mirror of
				https://gitee.com/dromara/hutool.git
				synced 2025-11-01 00:46:56 +08:00 
			
		
		
		
	修复DateUtil.rangeToList中step小于等于0时无限循环问题
This commit is contained in:
		| @@ -2,10 +2,11 @@ | |||||||
| # 🚀Changelog | # 🚀Changelog | ||||||
|  |  | ||||||
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | ||||||
| # 5.8.34(2024-11-05) | # 5.8.34(2024-11-06) | ||||||
|  |  | ||||||
| ### 🐣新特性 | ### 🐣新特性 | ||||||
| ### 🐞Bug修复 | ### 🐞Bug修复 | ||||||
|  | * 【core   】      修复DateUtil.rangeToList中step小于等于0时无限循环问题(issue#3783@Github) | ||||||
|  |  | ||||||
| ------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- | ||||||
| # 5.8.33(2024-11-05) | # 5.8.33(2024-11-05) | ||||||
|   | |||||||
| @@ -48,6 +48,10 @@ public class DateRange extends Range<DateTime> { | |||||||
| 	 */ | 	 */ | ||||||
| 	public DateRange(Date start, Date end, DateField unit, int step, boolean isIncludeStart, boolean isIncludeEnd) { | 	public DateRange(Date start, Date end, DateField unit, int step, boolean isIncludeStart, 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 cn.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()); | ||||||
|  | 	} | ||||||
|  | } | ||||||
		Reference in New Issue
	
	Block a user
	 Looly
					Looly