diff --git a/CHANGELOG.md b/CHANGELOG.md index b065ac8bb..1384206b2 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ------------------------------------------------------------------------------------------------------------- -# 5.8.8.M1 (2022-09-23) +# 5.8.8.M1 (2022-09-24) ### 🐣新特性 * 【core 】 StreamUtil.of方法新增对 Iterator 支持;StreamUtil.of(Iterable) 方法优化(pr#807@Gitee) @@ -19,6 +19,7 @@ * 【core 】 修复ObjectUtil.defaultIfNull去掉误加的deprecated(issue#I5SIZT@Gitee) * 【core 】 修复ReflectUtil 反射方法中桥接判断问题(issue#2625@Github) * 【poi 】 修复ExcelWriter导出List引起的个数混乱问题(issue#2627@Github) +* 【poi 】 修复ExcelReader读取时间变成12小时形式问题(issue#I5Q1TW@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java index bf160730e..24c1fc7c6 100755 --- a/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java +++ b/hutool-poi/src/main/java/cn/hutool/poi/excel/cell/values/NumericCellValue.java @@ -4,11 +4,12 @@ import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.poi.excel.ExcelDateUtil; import cn.hutool.poi.excel.cell.CellValue; -import java.util.Date; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.util.NumberToTextConverter; +import java.time.LocalDateTime; + /** * 数字类型单元格值
* 单元格值可能为Long、Double、Date @@ -37,10 +38,10 @@ public class NumericCellValue implements CellValue { if (null != style) { // 判断是否为日期 if (ExcelDateUtil.isDateFormat(cell)) { - // 1899年写入会导致数据错乱,读取到1899年证明这个单元格的信息不关注年月日 - Date dateCellValue = cell.getDateCellValue(); - if ("1899".equals(DateUtil.format(dateCellValue, "yyyy"))) { - return DateUtil.format(dateCellValue, style.getDataFormatString()); + final LocalDateTime dateCellValue = cell.getLocalDateTimeCellValue(); + if(1899 == dateCellValue.getYear()){ + // 1899年写入会导致数据错乱,读取到1899年证明这个单元格的信息不关注年月日 + return dateCellValue.toLocalTime(); } // 使用Hutool的DateTime包装 return DateUtil.date(dateCellValue); diff --git a/hutool-poi/src/test/java/cn/hutool/poi/IssueI5Q1TWTest.java b/hutool-poi/src/test/java/cn/hutool/poi/IssueI5Q1TWTest.java new file mode 100644 index 000000000..edbfad091 --- /dev/null +++ b/hutool-poi/src/test/java/cn/hutool/poi/IssueI5Q1TWTest.java @@ -0,0 +1,20 @@ +package cn.hutool.poi; + +import cn.hutool.poi.excel.ExcelReader; +import cn.hutool.poi.excel.ExcelUtil; +import org.junit.Assert; +import org.junit.Test; + +public class IssueI5Q1TWTest { + + @Test + public void readTest() { + final ExcelReader reader = ExcelUtil.getReader("I5Q1TW.xlsx"); + + // 自定义时间格式1 + Assert.assertEquals("18:56", reader.readCellValue(0, 0).toString()); + + // 自定义时间格式2 + Assert.assertEquals("18:56", reader.readCellValue(1, 0).toString()); + } +} diff --git a/hutool-poi/src/test/resources/I5Q1TW.xlsx b/hutool-poi/src/test/resources/I5Q1TW.xlsx new file mode 100644 index 000000000..7507bfe64 Binary files /dev/null and b/hutool-poi/src/test/resources/I5Q1TW.xlsx differ