fix convert bug

This commit is contained in:
Looly 2021-07-28 00:35:03 +08:00
parent fca211de07
commit c3f1fe50e4
7 changed files with 55 additions and 18 deletions

View File

@ -3,14 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.7.7 (2021-07-27)
### 🐣新特性
### 🐞Bug修复
-------------------------------------------------------------------------------------------------------------
# 5.7.6 (2021-07-26)
# 5.7.6 (2021-07-28)
### 🐣新特性
* 【core 】 增加FieldsComparatorpr#374@Gitee
@ -30,7 +23,9 @@
* 【core 】 修复FileTypeUtil判断wps修改过的xlsx误判为jar的问题pr#380@Gitee
* 【core 】 修复Sftp.isDir异常bugpr#378@Gitee
* 【core 】 修复BeanUtil.copyProperties集合元素复制成功读取失败的问题issue#I41WKP@Gitee
* 【core 】 修复NumberChineseFormatter.chineseToNumber十位数错误issue#1726@Gitee
* 【core 】 修复NumberChineseFormatter.chineseToNumber十位数错误issue#1726@github
* 【poi 】 修复BeanSheetReader.read中字段对象为空导致的报错issue#1729@Github
* 【core 】 修复DateConverter转换java.sql.Date问题issue#1729@Github
-------------------------------------------------------------------------------------------------------------

View File

@ -62,8 +62,7 @@ public class MapValueProvider implements ValueProvider<String> {
return null;
}
final Object value = map.get(key1);
return Convert.convertWithCheck(valueType, value, null, this.ignoreError);
return Convert.convertWithCheck(valueType, map.get(key1), null, this.ignoreError);
}
@Override

View File

@ -1,13 +1,13 @@
package cn.hutool.core.convert.impl;
import cn.hutool.core.convert.AbstractConverter;
import cn.hutool.core.convert.ConvertException;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;
import java.util.Date;
/**
* 日期转换器
@ -71,7 +71,7 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
} else {
// 统一按照字符串处理
final String valueStr = convertToStr(value);
final Date date = StrUtil.isBlank(this.format) //
final java.util.Date date = StrUtil.isBlank(this.format) //
? DateUtil.parse(valueStr) //
: DateUtil.parse(valueStr, this.format);
if(null != date){
@ -79,7 +79,7 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
}
}
throw new UnsupportedOperationException(StrUtil.format("Unsupport Date type: {}", this.targetType.getName()));
throw new ConvertException("Can not convert {}:[{}] to {}", value.getClass().getName(), value, this.targetType.getName());
}
/**
@ -105,7 +105,7 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
return new java.sql.Timestamp(date.getTime());
}
throw new UnsupportedOperationException(StrUtil.format("Unsupport Date type: {}", this.targetType.getName()));
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", this.targetType.getName()));
}
/**
@ -116,7 +116,7 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
private java.util.Date wrap(long mills){
// 返回指定类型
if (java.util.Date.class == targetType) {
return new Date(mills);
return new java.util.Date(mills);
}
if (DateTime.class == targetType) {
return DateUtil.date(mills);
@ -131,6 +131,12 @@ public class DateConverter extends AbstractConverter<java.util.Date> {
return new java.sql.Timestamp(mills);
}
throw new UnsupportedOperationException(StrUtil.format("Unsupport Date type: {}", this.targetType.getName()));
throw new UnsupportedOperationException(StrUtil.format("Unsupported target Date type: {}", this.targetType.getName()));
}
@SuppressWarnings("unchecked")
@Override
public Class<java.util.Date> getTargetType() {
return (Class<java.util.Date>) this.targetType;
}
}

View File

@ -2,6 +2,7 @@ package cn.hutool.core.convert;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateException;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.ByteUtil;
import lombok.AllArgsConstructor;
@ -310,4 +311,10 @@ public class ConvertTest {
final Date date = Convert.toDate("2021-01");
Assert.assertNull(date);
}
@Test
public void toSqlDateTest(){
final java.sql.Date date = Convert.convert(java.sql.Date.class, DateUtil.parse("2021-07-28"));
Assert.assertEquals("2021-07-28", date.toString());
}
}

View File

@ -42,7 +42,7 @@ public class BeanSheetReader<T> implements SheetReader<List<T>> {
final List<T> beanList = new ArrayList<>(mapList.size());
for (Map<String, Object> map : mapList) {
beanList.add(BeanUtil.toBean(map, this.beanClass));
beanList.add(BeanUtil.toBeanIgnoreError(map, this.beanClass));
}
return beanList;
}

View File

@ -0,0 +1,30 @@
package cn.hutool.poi.excel;
import lombok.Data;
import org.junit.Assert;
import org.junit.Test;
import java.util.List;
/**
* issue#1729@Github<br>
* 日期为空时返回""而非null因此会导致日期等字段的转换错误此处转bean时忽略错误
*/
public class Issue1729Test {
@Test
public void readTest() {
final ExcelReader reader = ExcelUtil.getReader("UserProjectDO.xlsx");
final List<UserProjectDO> read = reader.read(0, 1, UserProjectDO.class);
Assert.assertEquals("aa", read.get(0).getProjectName());
Assert.assertNull(read.get(0).getEndTrainTime());
Assert.assertEquals("2020-02-02", read.get(0).getEndTestTime().toString());
}
@Data
public static class UserProjectDO {
private String projectName;
private java.sql.Date endTrainTime;
private java.sql.Date endTestTime;
}
}

Binary file not shown.