1 21 22 package org.dbunit.dataset.datatype; 23 24 import org.dbunit.dataset.ITable; 25 26 import java.sql.*; 27 28 33 public class TimestampDataType extends AbstractDataType 34 { 35 TimestampDataType() 36 { 37 super("TIMESTAMP", Types.TIMESTAMP, Timestamp.class, false); 38 } 39 40 43 public Object typeCast(Object value) throws TypeCastException 44 { 45 if (value == null || value == ITable.NO_VALUE) 46 { 47 return null; 48 } 49 50 if (value instanceof java.sql.Timestamp ) 51 { 52 return value; 53 } 54 55 if (value instanceof java.util.Date ) 56 { 57 java.util.Date date = (java.util.Date )value; 58 return new java.sql.Timestamp (date.getTime()); 59 } 60 61 if (value instanceof Long ) 62 { 63 Long date = (Long )value; 64 return new java.sql.Timestamp (date.longValue()); 65 } 66 67 if (value instanceof String ) 68 { 69 String stringValue = (String )value; 70 71 if (stringValue.length() == 10) 73 { 74 try 75 { 76 long time = java.sql.Date.valueOf(stringValue).getTime(); 77 return new java.sql.Timestamp (time); 78 } 79 catch (IllegalArgumentException e) 80 { 81 } 83 } 84 85 try 86 { 87 return java.sql.Timestamp.valueOf(stringValue); 88 } 89 catch (IllegalArgumentException e) 90 { 91 throw new TypeCastException(value, this, e); 92 } 93 } 94 95 throw new TypeCastException(value, this); 96 } 97 98 public boolean isDateTime() 99 { 100 return true; 101 } 102 103 public Object getSqlValue(int column, ResultSet resultSet) 104 throws SQLException, TypeCastException 105 { 106 Timestamp value = resultSet.getTimestamp(column); 107 if (value == null || resultSet.wasNull()) 108 { 109 return null; 110 } 111 return value; 112 } 113 114 public void setSqlValue(Object value, int column, PreparedStatement statement) 115 throws SQLException, TypeCastException 116 { 117 statement.setTimestamp(column, (java.sql.Timestamp )typeCast(value)); 118 } 119 } 120 121 122 123 124 125 | Popular Tags |