diff --git a/CHANGELOG.md b/CHANGELOG.md index 0042e17dd..94e5c3c1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.5.3 (2020-12-06) +# 5.5.3 (2020-12-07) ### 新特性 * 【core 】 IdcardUtil增加行政区划83(issue#1277@Github) @@ -14,6 +14,9 @@ ### Bug修复 * 【cache 】 修复Cache中get重复misCount计数问题(issue#1281@Github) * 【poi 】 修复sax读取自定义格式单元格无法识别日期类型的问题(issue#1283@Github) +* 【core 】 修复CollUtil.get越界问题(issue#1292@Github) +* 【json 】 修复TemporalAccessorUtil无法格式化LocalDate带时间问题(issue#1289@Github) +* 【json 】 修复自定义日期格式的LocalDateTime没有包装引号问题(issue#1289@Github) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java index b56c239bd..aa73d40f5 100644 --- a/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/collection/CollUtil.java @@ -1870,7 +1870,7 @@ public class CollUtil { /** * Iterator转换为Enumeration *

- * Adapt the specified Iterator to the Enumeration interface. + * Adapt the specified {@link Iterator} to the {@link Enumeration} interface. * * @param 集合元素类型 * @param iter {@link Iterator} @@ -1883,7 +1883,7 @@ public class CollUtil { /** * Enumeration转换为Iterator *

- * Adapt the specified Enumeration to the Iterator interface + * Adapt the specified {@code Enumeration} to the {@code Iterator} interface * * @param 集合元素类型 * @param e {@link Enumeration} @@ -2186,7 +2186,7 @@ public class CollUtil { } // 检查越界 - if (index >= size) { + if (index >= size || index < 0) { return null; } diff --git a/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java b/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java index 3ecb8a5af..9d838f861 100644 --- a/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/date/TemporalAccessorUtil.java @@ -13,6 +13,7 @@ import java.time.ZonedDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.TemporalAccessor; import java.time.temporal.TemporalField; +import java.time.temporal.UnsupportedTemporalTypeException; /** * {@link TemporalAccessor} 工具类封装 @@ -54,7 +55,19 @@ public class TemporalAccessorUtil extends TemporalUtil{ formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME; } - return formatter.format(time); + + try { + return formatter.format(time); + } catch (UnsupportedTemporalTypeException e){ + if(time instanceof LocalDate && e.getMessage().contains("HourOfDay")){ + // 用户传入LocalDate,但是要求格式化带有时间部分,转换为LocalDateTime重试 + return formatter.format(((LocalDate) time).atStartOfDay()); + }else if(time instanceof LocalTime && e.getMessage().contains("YearOfEra")){ + // 用户传入LocalTime,但是要求格式化带有日期部分,转换为LocalDateTime重试 + return formatter.format(((LocalTime) time).atDate(LocalDate.now())); + } + throw e; + } } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/date/TemporalAccessorUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/date/TemporalAccessorUtilTest.java new file mode 100644 index 000000000..bf8501625 --- /dev/null +++ b/hutool-core/src/test/java/cn/hutool/core/date/TemporalAccessorUtilTest.java @@ -0,0 +1,23 @@ +package cn.hutool.core.date; + +import org.junit.Assert; +import org.junit.Test; + +import java.time.LocalDate; +import java.time.LocalTime; + +public class TemporalAccessorUtilTest { + + @Test + public void formatLocalDateTest(){ + final String format = TemporalAccessorUtil.format(LocalDate.of(2020, 12, 7), DatePattern.NORM_DATETIME_PATTERN); + Assert.assertEquals("2020-12-07 00:00:00", format); + } + + @Test + public void formatLocalTimeTest(){ + final String today = TemporalAccessorUtil.format(LocalDate.now(), DatePattern.NORM_DATE_PATTERN); + final String format = TemporalAccessorUtil.format(LocalTime.MIN, DatePattern.NORM_DATETIME_PATTERN); + Assert.assertEquals(today + " 00:00:00", format); + } +} diff --git a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java index 98e9c3264..177d97b06 100644 --- a/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java +++ b/hutool-json/src/main/java/cn/hutool/json/InternalJSONUtil.java @@ -240,11 +240,14 @@ final class InternalJSONUtil { */ private static String formatDate(Object dateObj, String format) { if (StrUtil.isNotBlank(format)) { + final String dateStr; if(dateObj instanceof TemporalAccessor){ - return TemporalAccessorUtil.format((TemporalAccessor) dateObj, format); + dateStr = TemporalAccessorUtil.format((TemporalAccessor) dateObj, format); + } else{ + dateStr = DateUtil.format(Convert.toDate(dateObj), format); } //用户定义了日期格式 - return JSONUtil.quote(DateUtil.format(Convert.toDate(dateObj), format)); + return JSONUtil.quote(dateStr); } //默认使用时间戳