KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > typeconverter > SqlDateConverter


1 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
2

3 package jodd.typeconverter;
4
5 import java.sql.Date JavaDoc;
6
7
8 /**
9  * Converts given object to java.sql.Date.
10  */

11 public class SqlDateConverter implements TypeConverter {
12
13     public static Date JavaDoc valueOf(Object JavaDoc value) {
14     
15         if (value == null) {
16             return null;
17         }
18     
19         if (value instanceof Date JavaDoc) {
20             return (Date JavaDoc) value;
21         }
22         if (value instanceof Number JavaDoc) {
23             return new Date JavaDoc(((Number JavaDoc) value).longValue());
24         }
25     
26         try {
27             return (Date.valueOf(value.toString()));
28         } catch (IllegalArgumentException JavaDoc iaex) {
29             throw new TypeConversionException(iaex);
30         }
31     }
32
33     public Object JavaDoc convert(Object JavaDoc value) {
34         return valueOf(value);
35     }
36     
37 }
38
Popular Tags