Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to handle Date formats when populating the bean using Apache's BeanUtilsBean.populate?

 
achsu subra
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi All,

I am using Apache's BeanUtilsBean.populate(Object employee, Map empMap) to automatically populate my bean. All values in empMap are in String format. The populate() automatically takes care of the type conversions from String to int, long, boolean when populating the bean. But does not handle Date formats. Is there any way so that BeanUtils automatically takes care of converting string to Date instead of we manually type casting string to date? Any inputs will be very helpful.

Getting this error because of the Date attribute ... "Exception in thread "main" java.lang.IllegalArgumentException: Cannot invoke Employee.setJoiningDate - argument type mismatch"

Thanks a lot..



regards,
achsu
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64843
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the following classes:

org.apache.commons.beanutils.ConvertUtils
org.apache.commons.beanutils.Converter

You can use them to register custom converters.
 
david lim
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May share how you had resolve this issue please?

Below are my code snipplet and the error it returns:




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)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic