Skip to content

Commit

Permalink
fix time, timestamp, SQL. Date type conversion problems (#2502)
Browse files Browse the repository at this point in the history
  • Loading branch information
ningyu1 authored and jerrick-zhu committed Sep 14, 2018
1 parent 2881738 commit 5968f1a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5968f1a

Please sign in to comment.