1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.dbutils.handlers.properties;
18
19 import java.sql.Timestamp;
20 import java.util.Date;
21
22 import org.apache.commons.dbutils.PropertyHandler;
23
24
25
26
27
28 public class DatePropertyHandler implements PropertyHandler {
29
30 private static final String JAVA_SQL_TIMESTAMP = "java.sql.Timestamp";
31 private static final String JAVA_SQL_TIME = "java.sql.Time";
32 private static final String JAVA_SQL_DATE = "java.sql.Date";
33
34 @Override
35 public Object apply(final Class<?> parameter, Object value) {
36 final String targetType = parameter.getName();
37 final Date dateValue = (Date) value;
38 final long time = dateValue.getTime();
39
40 if (JAVA_SQL_DATE.equals(targetType)) {
41 value = new java.sql.Date(time);
42 } else if (JAVA_SQL_TIME.equals(targetType)) {
43 value = new java.sql.Time(time);
44 } else if (JAVA_SQL_TIMESTAMP.equals(targetType)) {
45 value = new Timestamp(time);
46 }
47
48 return value;
49 }
50
51 @Override
52 public boolean match(final Class<?> parameter, final Object value) {
53 if (value instanceof Date) {
54 final String targetType = parameter.getName();
55 if (JAVA_SQL_DATE.equals(targetType)) {
56 return true;
57 }
58 if (JAVA_SQL_TIME.equals(targetType)) {
59 return true;
60 }
61 if (JAVA_SQL_TIMESTAMP.equals(targetType) && !Timestamp.class.isInstance(value)) {
62 return true;
63 }
64 }
65
66 return false;
67 }
68 }