1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.dataset.ITable; 25 26 import java.sql.PreparedStatement ; 27 import java.sql.ResultSet ; 28 import java.sql.SQLException ; 29 import java.sql.Types ; 30 31 36 public class DateDataType extends AbstractDataType 37 { 38 DateDataType() 39 { 40 super("DATE", Types.DATE, java.sql.Date .class, false); 41 } 42 43 46 public Object typeCast(Object 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 ) 54 { 55 return value; 56 } 57 58 if (value instanceof java.util.Date ) 59 { 60 java.util.Date date = (java.util.Date )value; 61 return new java.sql.Date (date.getTime()); 62 } 63 64 if (value instanceof Long ) 65 { 66 Long date = (Long )value; 67 return new java.sql.Date (date.longValue()); 68 } 69 70 if (value instanceof String ) 71 { 72 String stringValue = (String )value; 73 74 if (stringValue.length() > 10) 76 { 77 try 78 { 79 long time = java.sql.Timestamp.valueOf(stringValue).getTime(); 80 return new java.sql.Date (time); 81 } 83 catch (IllegalArgumentException e) 84 { 85 } 87 } 88 89 try 90 { 91 return java.sql.Date.valueOf(stringValue); 92 } 93 catch (IllegalArgumentException 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 getSqlValue(int column, ResultSet resultSet) 108 throws SQLException , TypeCastException 109 { 110 java.sql.Date 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 value, int column, PreparedStatement statement) 119 throws SQLException , TypeCastException 120 { 121 statement.setDate(column, (java.sql.Date )typeCast(value)); 122 } 123 } 124 125 126 127 128 129 | Popular Tags |