commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Bryce Fischer <br...@berzerker-soft.com>
Subject BeanUtils: Convert Date to String
Date Thu, 19 Aug 2004 02:44:31 GMT
I have two classes, defined as such:

public class Test {
   private Date prop3;
  // getters and setters removed for brevity
}

public class TestForm {
   private String prop3;
}

I want to copy properties from an instance of class Test to an instance 
of class TestForm. I'd like to be able to convert the date to a specific 
formatted string. I started with the following:

public static void main(String [] args) {
      Test test = new Test();
      test.setProp3(new Date());
     
      TestForm testForm = new TestForm();
      BeanUtils.copyProperties(testForm, test);
     
      System.out.println("testForm2 [" + testForm + "]");
}

With debug on, I get the following:
BeanUtilsBean.copyProperties(224) | BeanUtils.copyProperties(prop3 
[null], prop3 [Wed Aug 18 22:38:51 EDT 2004])
BeanUtilsBean.copyProperty(331) |   copyProperty(prop3 [null], prop3, 
Wed Aug 18 22:38:51 EDT 2004)
BeanUtilsBean.copyProperty(411) |     target propName=prop3, type=class 
java.lang.String, index=-1, key=null
BeanUtilsBean.copyProperty(443) |         USING CONVERTER 
org.apache.commons.beanutils.converters.StringConverter@6b7920
PropertyUtilsBean.setSimpleProperty(1756) | setSimpleProperty: Invoking 
method public void 
com.berzerkersoft.testbeanutils.TestForm.setProp3(java.lang.String) with 
value Wed Aug 18 22:38:51 EDT 2004 (class java.lang.String)

testForm2 [prop3 [Wed Aug 18 22:38:51 EDT 2004]]

Why would it be using the StringConverter? I tried creating a 
DateConverter, and registering it. The following shows my new Converter 
class and the code I used to test:

public class DateConverter implements Converter{
   private static Log log = LogFactory.getLog(DateConverter.class);
   private static DateFormat df =
       new SimpleDateFormat("yyyy.MM.dd");

   public Object convert(Class type, Object value) {
       if (value == null) {
           return null;
       } else if (type == Date.class) {
           return convertToDate(type, value);
       } else if (type == String.class) {
           return convertToString(type, value);
       }

       throw new ConversionException("Could not convert " +
                                     value.getClass().getName() + " to " +
                                     type.getName());
   }

   protected Object convertToDate(Class type, Object value) {
       if (value instanceof String) {
           try {

               return df.parse((String) value);
           } catch (Exception pe) {
               throw new ConversionException("Error converting String to 
Date");
           }
       }

       throw new ConversionException("Could not convert " +
                                     value.getClass().getName() + " to " +
                                     type.getName());
   }

   protected Object convertToString(Class type, Object value) {
       if (value instanceof Date) {
           try {
               return df.format(value);
           } catch (Exception e) {
               throw new ConversionException("Error converting Date to 
String");
           }
       }

       return value.toString();
   }
}

public static void main(String [] args) {
      Test test = new Test();
      test.setProp3(new Date());
      ConvertUtils.register(new DateConverter(), Date.class);
      TestForm testForm = new TestForm();
      BeanUtils.copyProperties(testForm, test);
     
      System.out.println("testForm2 [" + testForm + "]");
}

I get the same results. I know I must be missing something obvious here...

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message