1 4 package org.oddjob.util; 5 6 import java.text.ParseException ; 7 import java.util.Date ; 8 9 import org.apache.commons.beanutils.ConversionException; 10 import org.apache.commons.beanutils.Converter; 11 12 public class DateConverter implements Converter { 13 14 15 public Object convert(Class type, Object value) 16 throws ConversionException { 17 if (value == null) { 18 return null; 19 } 20 if (String .class.equals(type) && value instanceof Date ) { 21 return convertDate((Date ) value); 22 } 23 else if (Date .class.equals(type) && value instanceof String ) { 24 return convertString((String ) value); 25 } 26 else { 27 throw new ConversionException("Can not convert from [" 28 + value.getClass().getName() + "] to [" 29 + type.getName() + "]"); 30 } 31 } 32 33 protected String convertDate(Date date) { 34 return DateHelper.formatDate(date); 35 } 36 37 protected Date convertString(String text) { 38 try { 39 return DateHelper.parseDateTime(text); 40 } catch (ParseException e) { 41 throw new ConversionException(e); 42 } 43 } 44 45 } 46 | Popular Tags |