You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Bryce Fischer <br...@berzerker-soft.com> on 2004/08/19 04:44:31 UTC
BeanUtils: Convert Date to String
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
Re: BeanUtils: Convert Date to String
Posted by Bryce Fischer <br...@berzerker-soft.com>.
Well.. I'm answering my own question. I guess I needed to create my own
StringConverter class:
public class MyStringConverter implements Converter {
private static DateFormat df = new SimpleDateFormat("yyyy.MM.dd");
public Object convert(Class type, Object value) {
Object returnObj = null;
if (value != null) {
if (value instanceof Date){
returnObj = df.format((Date)value);
} else {
returnObj = value.toString();
}
}
return returnObj;
}
}
Then I just registered the above class:
ConvertUtils.register(new MyStringConverter(), String.class);
Hope this can help someone else who encounters the same thing.
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org