1 package org.appfuse.util; 2 3 import java.text.DateFormat ; 4 import java.text.SimpleDateFormat ; 5 import java.util.Date ; 6 7 import org.apache.commons.beanutils.ConversionException; 8 import org.apache.commons.lang.StringUtils; 9 10 21 public class TimestampConverter extends DateConverter { 22 public static final String TS_FORMAT = DateUtil.getDatePattern() + " HH:mm:ss.S"; 23 24 protected Object convertToDate(Class type, Object value) { 25 DateFormat df = new SimpleDateFormat (TS_FORMAT); 26 if (value instanceof String ) { 27 try { 28 if (StringUtils.isEmpty(value.toString())) { 29 return null; 30 } 31 32 return df.parse((String ) value); 33 } catch (Exception pe) { 34 throw new ConversionException("Error converting String to Timestamp"); 35 } 36 } 37 38 throw new ConversionException("Could not convert " 39 + value.getClass().getName() + " to " + type.getName()); 40 } 41 42 protected Object convertToString(Class type, Object value) { 43 DateFormat df = new SimpleDateFormat (TS_FORMAT); 44 if (value instanceof Date ) { 45 try { 46 return df.format(value); 47 } catch (Exception e) { 48 throw new ConversionException("Error converting Timestamp to String"); 49 } 50 } 51 52 return value.toString(); 53 } 54 } | Popular Tags |