1 16 17 18 package org.apache.commons.beanutils.converters; 19 20 21 import java.sql.Timestamp ; 22 import org.apache.commons.beanutils.ConversionException; 23 import org.apache.commons.beanutils.Converter; 24 25 26 36 37 public final class SqlTimestampConverter implements Converter { 38 39 40 42 43 47 public SqlTimestampConverter() { 48 49 this.defaultValue = null; 50 this.useDefault = false; 51 52 } 53 54 55 61 public SqlTimestampConverter(Object defaultValue) { 62 63 this.defaultValue = defaultValue; 64 this.useDefault = true; 65 66 } 67 68 69 71 72 75 private Object defaultValue = null; 76 77 78 81 private boolean useDefault = true; 82 83 84 86 87 97 public Object convert(Class type, Object value) { 98 99 if (value == null) { 100 if (useDefault) { 101 return (defaultValue); 102 } else { 103 throw new ConversionException("No value specified"); 104 } 105 } 106 107 if (value instanceof Timestamp ) { 108 return (value); 109 } 110 111 try { 112 return (Timestamp.valueOf(value.toString())); 113 } catch (Exception e) { 114 if (useDefault) { 115 return (defaultValue); 116 } else { 117 throw new ConversionException(e); 118 } 119 } 120 121 } 122 123 124 } 125 | Popular Tags |