TechFundu

  • Increase font size
  • Default font size
  • Decrease font size
Welcome Guest,  Login | Join Now
Home Forum

BeanUtils.copyProperties(formBean, javaBean) not converting String dates to java.util.Date dates
(1 viewing) (1) Guest
Java Standard Edition development related issues discussion forum.
Go to bottomPage: 1
TOPIC: BeanUtils.copyProperties(formBean, javaBean) not converting String dates to java.util.Date dates
#75
BeanUtils.copyProperties(formBean, javaBean) not converting String dates to java.util.Date dates 1 Year, 11 Months ago Karma: 0
BeanUtils.copyProperties(formBean, javaBean) not converting String properties into Dates:

I am trying to figure out what is wrong with what I am doing.
I have a form bean with date string properties (from and to) which I would like to copy into a javabean class.

Thanks in advance.
roselin
Fresh Boarder
Posts: 9
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
#76
Re:BeanUtils.copyProperties(formBean, javaBean) not converting String dates to java.util.Date dates 1 Year, 11 Months ago Karma: 0
I got the solution.The solution that works is below.
Just create the two classes below and register them in the action like I do below and you should be all set.

To use these converters so that BeanUtils.copyProperties( ) works in
both directions I just added a static block to the top of my dispatch
Action ...

static {
DateBeanUtilsConverter dateConverter = new DateBeanUtilsConverter();
dateConverter.setFormatPattern( "MMddyyyy" );
StringBeanUtilsConverterDate myStringConverter = new StringBeanUtilsConverterDate();
myStringConverter.setFormatPattern( "MMddyyyy" );
ConvertUtils.register( dateConverter, java.util.Date.class );
ConvertUtils.register( myStringConverter, String.class );
}

The two classes are listed below. All seems to be working fine.

/** coverts java.util.Date to String using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import java.text.*;
import java.util.*;
import corporate.*;

public class DateBeanUtilsConverter implements Converter {

private String formatPattern = null;

public void setFormatPattern(String formatPattern) {
this.formatPattern = formatPattern;
}

public Object convert(Class type, Object value) {
Date date = null;

if (value != null
&& (value instanceof String)
&& (type == Date.class)) {
try {

String s = value.toString();
SimpleDateFormat formatter =
new SimpleDateFormat(formatPattern);
date = formatter.parse(s);

} catch (Exception e) {
ErrorLogging.println("DateBeanUtilsConverter: " + e);
}
}
return date;
}
}

/** coverts String to java.util.Date using BeanUtils ***/

import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.*;
import java.text.*;
import java.util.*;
import corporate.*;

public class StringBeanUtilsConverterDate implements Converter {
private static final StringConverter stringConverter =
new StringConverter();
private String formatPattern = null;

public void setFormatPattern(String formatPattern) {
this.formatPattern = formatPattern;
}

public Object convert(Class type, Object value) {
Object returnValue = null;

if (value != null) {
if (type == String.class && (value instanceof Date)) {
SimpleDateFormat formatter =
new SimpleDateFormat(formatPattern);
String dateString = formatter.format(value);
returnValue = dateString;
} else {
returnValue = stringConverter.convert(type, value);
}
}
return returnValue;
}
}
roselin
Fresh Boarder
Posts: 9
graphgraph
User Offline Click here to see the profile of this user
The administrator has disabled public write access.
 
Go to topPage: 1
get the latest posts directly to your desktop
Follow us on Twitter