1
0
mirror of https://gitee.com/dromara/hutool.git synced 2025-04-05 17:37:59 +08:00

修复ExcelReader读取时间变成12小时形式问题

This commit is contained in:
Looly 2022-09-24 01:04:16 +08:00
parent f51df24970
commit 9dfac34f69
4 changed files with 28 additions and 6 deletions
CHANGELOG.md
hutool-poi/src
main/java/cn/hutool/poi/excel/cell/values
test
java/cn/hutool/poi
resources

View File

@ -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去掉误加的deprecatedissue#I5SIZT@Gitee
* 【core 】 修复ReflectUtil 反射方法中桥接判断问题issue#2625@Github
* 【poi 】 修复ExcelWriter导出List<Map>引起的个数混乱问题issue#2627@Github
* 【poi 】 修复ExcelReader读取时间变成12小时形式问题issue#I5Q1TW@Gitee
-------------------------------------------------------------------------------------------------------------

View File

@ -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;
/**
* 数字类型单元格值<br>
* 单元格值可能为LongDoubleDate
@ -37,10 +38,10 @@ public class NumericCellValue implements CellValue<Object> {
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);

View File

@ -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());
}
}

Binary file not shown.