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

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