diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java index 3f9356f21f1..637e11f15b1 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java @@ -79,9 +79,18 @@ public static Object compatibleTypeConvert(Object value, Class type) { return new Byte(string); } else if (type == Boolean.class || type == boolean.class) { return new Boolean(string); - } else if (type == Date.class) { + } else if (type == Date.class || type == java.sql.Date.class || type == java.sql.Timestamp.class || type == java.sql.Time.class) { try { - return new SimpleDateFormat(DATE_FORMAT).parse((String) value); + Date date = new SimpleDateFormat(DATE_FORMAT).parse((String) value); + if (type == java.sql.Date.class) { + return new java.sql.Date(date.getTime()); + } else if (type == java.sql.Timestamp.class) { + return new java.sql.Timestamp(date.getTime()); + } else if (type == java.sql.Time.class) { + return new java.sql.Time(date.getTime()); + } else { + return date; + } } catch (ParseException e) { throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e); } diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java index 63efc5b9542..db0db8a3126 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java @@ -68,6 +68,15 @@ public void testCompatibleTypeConvert() throws Exception { result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", Date.class); assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-11 12:24:12"), (Date) result); + result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Date.class); + assertEquals(new SimpleDateFormat("yyyy-MM-dd").format((java.sql.Date) result), "2011-12-11"); + + result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Time.class); + assertEquals(new SimpleDateFormat("HH:mm:ss").format((java.sql.Time) result), "12:24:12"); + + result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Timestamp.class); + assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.sql.Timestamp) result), "2011-12-11 12:24:12"); + result = CompatibleTypeUtils.compatibleTypeConvert("ab", char[].class); assertEquals(2, ((char[]) result).length); assertEquals('a', ((char[]) result)[0]); diff --git a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java index d8b458ee787..54e223a652f 100644 --- a/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java +++ b/dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java @@ -29,8 +29,10 @@ import java.lang.reflect.Method; import java.lang.reflect.Type; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.LinkedList; @@ -559,6 +561,34 @@ public void testListPojoListPojo() throws Exception { Assert.assertEquals(parent.getAge(), realizeParent.getAge()); } + @Test + public void testDateTimeTimestamp() throws Exception { + String dateStr = "2018-09-12"; + String timeStr = "10:12:33"; + String dateTimeStr = "2018-09-12 10:12:33"; + String[] dateFormat = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"}; + + //java.util.Date + Object date = PojoUtils.realize(dateTimeStr, Date.class, (Type) Date.class); + assertEquals(Date.class, date.getClass()); + assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(date)); + + //java.sql.Time + Object time = PojoUtils.realize(dateTimeStr, java.sql.Time.class, (Type) java.sql.Time.class); + assertEquals(java.sql.Time.class, time.getClass()); + assertEquals(timeStr, new SimpleDateFormat(dateFormat[2]).format(time)); + + //java.sql.Date + Object sqlDate = PojoUtils.realize(dateTimeStr, java.sql.Date.class, (Type) java.sql.Date.class); + assertEquals(java.sql.Date.class, sqlDate.getClass()); + assertEquals(dateStr, new SimpleDateFormat(dateFormat[1]).format(sqlDate)); + + //java.sql.Timestamp + Object timestamp = PojoUtils.realize(dateTimeStr, java.sql.Timestamp.class, (Type) java.sql.Timestamp.class); + assertEquals(java.sql.Timestamp.class, timestamp.getClass()); + assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp)); + } + public static class Parent { public String gender; public String email;