Java BeanUtilsBean : Convert Date to String

22,281

Solution 1

Done using this method:

public Object populate(Object obj, HashMap<String, String[]> formData)
            throws IllegalAccessException, InvocationTargetException {
        ConvertUtils
                .register(new StringToDateConverter(), java.util.Date.class);
        BeanUtilsBean.getInstance().populate(obj, formData);
        return obj;
    }

Solution 2

You need a SimpleDateFormat by which to parse the given string according to a specified format. For that you'd need to handle the conversion manually - name the request parameter differently and then set it manually.

But beanutils has a conversion utility, so you can use it instead (this code can be executed once per application):

DateTimeConverter dtConverter = new DateConverter();
dtConverter.setPattern("<your custom date pattern here>");
ConvertUtils.register(dtConverter, Date.class);
Share:
22,281
d4v1dv00
Author by

d4v1dv00

Updated on April 25, 2020

Comments

  • d4v1dv00
    d4v1dv00 about 4 years

    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<String, String[]> formData = new HashMap<String, String[]>();
        formData.put("email", new String[]{"[email protected]"});
        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)

  • d4v1dv00
    d4v1dv00 about 13 years
    I just uploaded the codes, which part I should put in SimpleDateFormat?
  • d4v1dv00
    d4v1dv00 about 13 years
    very funny, DateTimeConverter dtConverter = new DateTimeConverter(); is not possible to be instantiated.
  • Bozho
    Bozho about 13 years
    sorry, see updated. it's DateConverter that's the concrete class
  • d4v1dv00
    d4v1dv00 about 13 years
    Sorry mate, I had updated the code like above but it still the same error.
  • Doom
    Doom almost 11 years
    i have a question, if value of date is null send exception. How can solve? thanks.