Return-Path: Delivered-To: apmail-jakarta-commons-user-archive@www.apache.org Received: (qmail 83815 invoked from network); 19 Aug 2004 02:39:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (209.237.227.199) by minotaur-2.apache.org with SMTP; 19 Aug 2004 02:39:01 -0000 Received: (qmail 68972 invoked by uid 500); 19 Aug 2004 02:38:52 -0000 Delivered-To: apmail-jakarta-commons-user-archive@jakarta.apache.org Received: (qmail 68910 invoked by uid 500); 19 Aug 2004 02:38:51 -0000 Mailing-List: contact commons-user-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Users List" Reply-To: "Jakarta Commons Users List" Delivered-To: mailing list commons-user@jakarta.apache.org Received: (qmail 68896 invoked by uid 99); 19 Aug 2004 02:38:51 -0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received: from [66.45.226.248] (HELO berzerker-soft.com) (66.45.226.248) by apache.org (qpsmtpd/0.27.1) with ESMTP; Wed, 18 Aug 2004 19:38:50 -0700 Received: from [127.0.0.1] (dt0a0na0.tampabay.rr.com [24.92.23.160]) by berzerker-soft.com (Postfix) with ESMTP id 8E75F84699 for ; Wed, 18 Aug 2004 22:19:02 -0400 (EDT) Message-ID: <4124140F.8010209@berzerker-soft.com> Date: Wed, 18 Aug 2004 22:44:31 -0400 From: Bryce Fischer User-Agent: Mozilla Thunderbird 0.7.2 (Windows/20040707) X-Accept-Language: en-us, en MIME-Version: 1.0 To: commons-user@jakarta.apache.org Subject: BeanUtils: Convert Date to String Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N 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