BeanUtils.populate () solution to convert the empty string date

When we use BeanUtils.populate () parameter package, if the package is an empty string, occurs when the date is converted into the above exception, may be added at this time static code block in a tool class can be solved: Note introduced package beanutils

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.SqlDateConverter;

 

static {
ConvertUtils.register(new SqlDateConverter(null), java.sql.Date.class);

ConvertUtils.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try{
return simpleDateFormat.parse(value.toString());
} catch (ParseException e) {
e.printStackTrace();
}
return null;
}
}, Date.class);
}

Guess you like

Origin www.cnblogs.com/xiaoshenke/p/10953281.html