Skip to content

Commit

Permalink
Support the compatible conversion for subtypes of Date. (sofastack#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
leyou240 authored and ujjboy committed Sep 18, 2018
1 parent a8eec7b commit 1c704b6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
* @author <a href=mailto:[email protected]>GengZhang</a>
*/
public class ExtensionLoaderFactory {
private ExtensionLoaderFactory() {
}

/**
* All extension loader {Class : ExtensionLoader}
*/
public static final ConcurrentMap<Class, ExtensionLoader> LOADER_MAP = new ConcurrentHashMap<Class, ExtensionLoader>();
private static final ConcurrentMap<Class, ExtensionLoader> LOADER_MAP = new ConcurrentHashMap<Class, ExtensionLoader>();

/**
* Get extension loader by extensible class with listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ private CompatibleTypeUtils() {
* <li> Number -&gt; Number </li>
* <li> List -&gt; Array </li>
* </ul>
*
* @param value 原始值
* @param type 目标类型
* @param type 目标类型
* @return 目标值
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down Expand Up @@ -81,9 +82,18 @@ public static Object convert(Object value, Class<?> type) {
return Byte.valueOf(string);
} else if (type == Boolean.class || type == boolean.class) {
return Boolean.valueOf(string);
} else if (type == Date.class) {
} else if (type == Date.class || type == java.sql.Date.class || type == java.sql.Time.class ||
type == java.sql.Timestamp.class) {
try {
return DateUtils.strToDate(string, DateUtils.DATE_FORMAT_TIME);
if (type == Date.class) {
return DateUtils.strToDate(string, DateUtils.DATE_FORMAT_TIME);
} else if (type == java.sql.Date.class) {
return new java.sql.Date(DateUtils.strToLong(string));
} else if (type == java.sql.Timestamp.class) {
return new java.sql.Timestamp(DateUtils.strToLong(string));
} else if (type == java.sql.Time.class) {
return new java.sql.Time(DateUtils.strToLong(string));
}
} catch (ParseException e) {
throw new IllegalStateException("Failed to parse date " + value + " by format " +
DateUtils.DATE_FORMAT_TIME + ", cause: " + e.getMessage(), e);
Expand Down Expand Up @@ -111,6 +121,12 @@ public static Object convert(Object value, Class<?> type) {
return BigDecimal.valueOf(number.doubleValue());
} else if (type == Date.class) {
return new Date(number.longValue());
} else if (type == java.sql.Date.class) {
return new java.sql.Date(number.longValue());
} else if (type == java.sql.Time.class) {
return new java.sql.Time(number.longValue());
} else if (type == java.sql.Timestamp.class) {
return new java.sql.Timestamp(number.longValue());
}
} else if (value instanceof Collection) {
Collection collection = (Collection) value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ public static Date strToDate(String dateStr) throws ParseException {
return strToDate(dateStr, DATE_FORMAT_TIME);
}

/**
* 字符串转时间戳
* @param dateStr 时间字符串
* @return 时间戳
* @throws ParseException 解析异常
*/
public static Long strToLong(String dateStr) throws ParseException {
return strToDate(dateStr).getTime();
}

/**
* 字符串转时间
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import java.util.Set;

/**
*
*
* @author <a href="mailto:[email protected]">GengZhang</a>
*/
public class CompatibleTypeUtilsTest {
Expand Down Expand Up @@ -65,6 +63,18 @@ public void convert() throws Exception {

Date dataTime = DateUtils.strToDate("2018-1-1 11:22:33");
Assert.assertEquals(dataTime, CompatibleTypeUtils.convert("2018-1-1 11:22:33", Date.class));
Long timeLong = DateUtils.strToLong("2018-1-1 11:22:33");
java.sql.Date sqlDate = new java.sql.Date(timeLong);
Object timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Date.class);
Assert.assertEquals(sqlDate, timeResult);
timeResult = CompatibleTypeUtils.convert(timeLong, java.sql.Date.class);
Assert.assertEquals(sqlDate, timeResult);
timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Timestamp.class);
java.sql.Timestamp timestamp = new java.sql.Timestamp(timeLong);
Assert.assertEquals(timestamp, timeResult);
timeResult = CompatibleTypeUtils.convert("2018-1-1 11:22:33", java.sql.Time.class);
java.sql.Time time = new java.sql.Time(timeLong);
Assert.assertEquals(time, timeResult);

Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert(123, Short.class));
Assert.assertEquals(new Short("123"), CompatibleTypeUtils.convert(123, short.class));
Expand Down

0 comments on commit 1c704b6

Please sign in to comment.