KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > datatype > DateDataType


1 /*
2  *
3  * The DbUnit Database Testing Framework
4  * Copyright (C)2002-2004, DbUnit.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */

21
22 package org.dbunit.dataset.datatype;
23
24 import org.dbunit.dataset.ITable;
25
26 import java.sql.PreparedStatement JavaDoc;
27 import java.sql.ResultSet JavaDoc;
28 import java.sql.SQLException JavaDoc;
29 import java.sql.Types JavaDoc;
30
31 /**
32  * @author Manuel Laflamme
33  * @version $Revision: 1.16 $
34  * @since Feb 19, 2002
35  */

36 public class DateDataType extends AbstractDataType
37 {
38     DateDataType()
39     {
40         super("DATE", Types.DATE, java.sql.Date JavaDoc.class, false);
41     }
42
43     ////////////////////////////////////////////////////////////////////////////
44
// DataType class
45

46     public Object JavaDoc typeCast(Object JavaDoc value) throws TypeCastException
47     {
48         if (value == null || value == ITable.NO_VALUE)
49         {
50             return null;
51         }
52
53         if (value instanceof java.sql.Date JavaDoc)
54         {
55             return value;
56         }
57
58         if (value instanceof java.util.Date JavaDoc)
59         {
60             java.util.Date JavaDoc date = (java.util.Date JavaDoc)value;
61             return new java.sql.Date JavaDoc(date.getTime());
62         }
63
64         if (value instanceof Long JavaDoc)
65         {
66             Long JavaDoc date = (Long JavaDoc)value;
67             return new java.sql.Date JavaDoc(date.longValue());
68         }
69
70         if (value instanceof String JavaDoc)
71         {
72             String JavaDoc stringValue = (String JavaDoc)value;
73
74             // Probably a Timestamp, try it just in case!
75
if (stringValue.length() > 10)
76             {
77                 try
78                 {
79                     long time = java.sql.Timestamp.valueOf(stringValue).getTime();
80                     return new java.sql.Date JavaDoc(time);
81 // return java.sql.Date.valueOf(new java.sql.Date(time).toString());
82
}
83                 catch (IllegalArgumentException JavaDoc e)
84                 {
85                     // Was not a Timestamp, let java.sql.Date handle this value
86
}
87             }
88
89             try
90             {
91                 return java.sql.Date.valueOf(stringValue);
92             }
93             catch (IllegalArgumentException JavaDoc e)
94             {
95                 throw new TypeCastException(value, this, e);
96             }
97         }
98
99         throw new TypeCastException(value, this);
100     }
101
102     public boolean isDateTime()
103     {
104         return true;
105     }
106
107     public Object JavaDoc getSqlValue(int column, ResultSet JavaDoc resultSet)
108             throws SQLException JavaDoc, TypeCastException
109     {
110         java.sql.Date JavaDoc value = resultSet.getDate(column);
111         if (value == null || resultSet.wasNull())
112         {
113             return null;
114         }
115         return value;
116     }
117
118     public void setSqlValue(Object JavaDoc value, int column, PreparedStatement JavaDoc statement)
119             throws SQLException JavaDoc, TypeCastException
120     {
121         statement.setDate(column, (java.sql.Date JavaDoc)typeCast(value));
122     }
123 }
124
125
126
127
128
129
Popular Tags