dcsimg
Java BeanUtilsBean : Convert Date to String
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Neo_Gigs
Posted On:   Saturday, April 23, 2011 09:49 AM

I am trying to run BeanUtilsBean.getInstance().populate(...) but on the HTML form, there is a field that carries String representation of Date of Birth. The object bean has the field type of java.util.Date Read some search from Ggl that have to build custom converters but not quite understand how to do that. Anyone can help? My code: public static void main(String[] args) { Map formData = new HashMap (); formData.put("email", new String[]{"danny@gmail.com"}); formData.put("firstName", new String[]{"danny"}); formData.put("lastName", new String[]{"miller"}); formData.put("do   More>>

I am trying to run BeanUtilsBean.getInstance().populate(...) but on the HTML form, there is a field that carries String representation of Date of Birth. The object bean has the field type of java.util.Date


Read some search from Ggl that have to build custom converters but not quite understand how to do that.


Anyone can help?


My code:

			
public static void main(String[] args) {
Map formData = new HashMap ();
formData.put("email", new String[]{"danny@gmail.com"});
formData.put("firstName", new String[]{"danny"});
formData.put("lastName", new String[]{"miller"});
formData.put("dob", new String[]{"15-Apr-1980"});
formData.put("userName", new String[]{"dannymiller"});
try {
Consumer consumer = new Consumer();
DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPattern("dd/MMM/yyyy");

ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
convertUtilsBean.deregister(Date.class);
convertUtilsBean.register(dtConverter, Date.class);

BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean, new PropertyUtilsBean());

beanUtilsBean.populate(consumer, formData);


if (consumer != null) {
System.out.println(consumer.getEmail());
System.out.println(consumer.getFirstName());
System.out.println(consumer.getLastName());
System.out.println(consumer.getDob());
System.out.println(consumer.getUserName());
}
} catch (Exception e) {
e.printStackTrace();
}
}


The return error:



Apr 22, 2011 11:14:45 PM org.apache.commons.beanutils.converters.DateTimeConverter toDate WARNING: DateConverter does not support default String to 'Date' conversion. Apr 22, 2011 11:14:45 PM org.apache.commons.beanutils.converters.DateTimeConverter toDate WARNING: (N.B. Re-configure Converter or use alternative implementation) Exception in thread "main" org.apache.commons.beanutils.ConversionException: DateConverter does not support default String to 'Date' conversion. at org.apache.commons.beanutils.converters.DateTimeConverter.toDate(DateTimeConverter.java:468) at org.apache.commons.beanutils.converters.DateTimeConverter.convertToType(DateTimeConverter.java:343) at org.apache.commons.beanutils.converters.AbstractConverter.convert(AbstractConverter.java:156) at org.apache.commons.beanutils.converters.ConverterFacade.convert(ConverterFacade.java:60) at org.apache.commons.beanutils.ConvertUtilsBean.convert(ConvertUtilsBean.java:470) at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1008) at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830) at com.ymatch.test.BeanTest.main(BeanTest.java:32)
   <<Less

Re: Java BeanUtilsBean : Convert Date to String

Posted By:   aaronabbott58  
Posted On:   Friday, July 6, 2012 06:15 AM

If the object is serializable at all, then  the reconstruction should be a faithful copy. In normal uses of
serialization, the original object is nowhere near; it could be on  the other side of the world at the far end of a network  connection. So you can be sure that changing the copy will have
no effect on the original.  orbital-maldives-holidays.co.uk

About | Sitemap | Contact