Skip to content

Commit 5968f1a

Browse files
ningyu1jerrick-zhu
authored andcommitted
fix time, timestamp, SQL. Date type conversion problems (#2502)
1 parent 2881738 commit 5968f1a

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,18 @@ public static Object compatibleTypeConvert(Object value, Class<?> type) {
7979
return new Byte(string);
8080
} else if (type == Boolean.class || type == boolean.class) {
8181
return new Boolean(string);
82-
} else if (type == Date.class) {
82+
} else if (type == Date.class || type == java.sql.Date.class || type == java.sql.Timestamp.class || type == java.sql.Time.class) {
8383
try {
84-
return new SimpleDateFormat(DATE_FORMAT).parse((String) value);
84+
Date date = new SimpleDateFormat(DATE_FORMAT).parse((String) value);
85+
if (type == java.sql.Date.class) {
86+
return new java.sql.Date(date.getTime());
87+
} else if (type == java.sql.Timestamp.class) {
88+
return new java.sql.Timestamp(date.getTime());
89+
} else if (type == java.sql.Time.class) {
90+
return new java.sql.Time(date.getTime());
91+
} else {
92+
return date;
93+
}
8594
} catch (ParseException e) {
8695
throw new IllegalStateException("Failed to parse date " + value + " by format " + DATE_FORMAT + ", cause: " + e.getMessage(), e);
8796
}

dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,15 @@ public void testCompatibleTypeConvert() throws Exception {
6868
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", Date.class);
6969
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-11 12:24:12"), (Date) result);
7070

71+
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Date.class);
72+
assertEquals(new SimpleDateFormat("yyyy-MM-dd").format((java.sql.Date) result), "2011-12-11");
73+
74+
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Time.class);
75+
assertEquals(new SimpleDateFormat("HH:mm:ss").format((java.sql.Time) result), "12:24:12");
76+
77+
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", java.sql.Timestamp.class);
78+
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format((java.sql.Timestamp) result), "2011-12-11 12:24:12");
79+
7180
result = CompatibleTypeUtils.compatibleTypeConvert("ab", char[].class);
7281
assertEquals(2, ((char[]) result).length);
7382
assertEquals('a', ((char[]) result)[0]);

dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/PojoUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929

3030
import java.lang.reflect.Method;
3131
import java.lang.reflect.Type;
32+
import java.text.SimpleDateFormat;
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
35+
import java.util.Date;
3436
import java.util.HashMap;
3537
import java.util.LinkedHashMap;
3638
import java.util.LinkedList;
@@ -559,6 +561,34 @@ public void testListPojoListPojo() throws Exception {
559561
Assert.assertEquals(parent.getAge(), realizeParent.getAge());
560562
}
561563

564+
@Test
565+
public void testDateTimeTimestamp() throws Exception {
566+
String dateStr = "2018-09-12";
567+
String timeStr = "10:12:33";
568+
String dateTimeStr = "2018-09-12 10:12:33";
569+
String[] dateFormat = new String[]{"yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd", "HH:mm:ss"};
570+
571+
//java.util.Date
572+
Object date = PojoUtils.realize(dateTimeStr, Date.class, (Type) Date.class);
573+
assertEquals(Date.class, date.getClass());
574+
assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(date));
575+
576+
//java.sql.Time
577+
Object time = PojoUtils.realize(dateTimeStr, java.sql.Time.class, (Type) java.sql.Time.class);
578+
assertEquals(java.sql.Time.class, time.getClass());
579+
assertEquals(timeStr, new SimpleDateFormat(dateFormat[2]).format(time));
580+
581+
//java.sql.Date
582+
Object sqlDate = PojoUtils.realize(dateTimeStr, java.sql.Date.class, (Type) java.sql.Date.class);
583+
assertEquals(java.sql.Date.class, sqlDate.getClass());
584+
assertEquals(dateStr, new SimpleDateFormat(dateFormat[1]).format(sqlDate));
585+
586+
//java.sql.Timestamp
587+
Object timestamp = PojoUtils.realize(dateTimeStr, java.sql.Timestamp.class, (Type) java.sql.Timestamp.class);
588+
assertEquals(java.sql.Timestamp.class, timestamp.getClass());
589+
assertEquals(dateTimeStr, new SimpleDateFormat(dateFormat[0]).format(timestamp));
590+
}
591+
562592
public static class Parent {
563593
public String gender;
564594
public String email;

0 commit comments

Comments
 (0)