使用BeanUtils时,Date类型值为空的解决方法

本文介绍了一种解决Java中org.apache.commons.beanutils.ConversionException异常的方法,通过自定义日期转换器来处理日期格式不匹配的问题,并展示了如何使用ExtBeanUtils进行对象属性复制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

解决办法参考:[url]http://hi.baidu.com/fcp_bd/blog/item/0e632783c08836a50cf4d2c4.html/cmtid/53484428cab979f399250ad7[/url]

[color=red]org.apache.commons.beanutils.ConversionException: No value specified for 'Date'[/color]

package com.asl.cityu.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.Converter;

public class DateConvert implements Converter {
private static String dateFormatStr = "yyyy/MM/dd";
private static SimpleDateFormat dateTimeFormat = new SimpleDateFormat(dateFormatStr);

private static String dateLongFormatStr = dateFormatStr+" HH:mm:ss";
private static SimpleDateFormat dateTimeLongFormat = new SimpleDateFormat(dateLongFormatStr);

public Object convert(Class arg0, Object arg1) {
System.out.println(arg1.getClass().getName()+"="+arg1.toString());
String className = arg1.getClass().getName();
//java.sql.Timestamp
if ("java.sql.Timestamp".equalsIgnoreCase(className)) {
try {
SimpleDateFormat df = new SimpleDateFormat(dateFormatStr + " HH:mm:ss");
return df.parse(dateTimeLongFormat.format(arg1));
} catch (Exception e) {
try {
SimpleDateFormat df = new SimpleDateFormat(dateFormatStr);
return df.parse(dateTimeFormat.format(arg1));
} catch (ParseException ex) {
e.printStackTrace();
return null;
}
}
}else{//java.util.Date,java.sql.Date
String p = (String) arg1;
if (p == null || p.trim().length() == 0) {
return null;
}
try {
SimpleDateFormat df = new SimpleDateFormat(dateFormatStr + " HH:mm:ss");
return df.parse(p.trim());
} catch (Exception e) {
try {
SimpleDateFormat df = new SimpleDateFormat(dateFormatStr);
return df.parse(p.trim());
} catch (ParseException ex) {
e.printStackTrace();
return null;
}
}
}
}

public static String formatDateTime(Object obj) {
if (obj != null)
return dateTimeFormat.format(obj);
else
return "";
}

public static String formatLongDateTime(Object obj) {
if (obj != null)
return dateTimeLongFormat.format(obj);
else
return "";
}

}





package com.asl.cityu.common;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;

public class ExtBeanUtils extends BeanUtils {
static {
ConvertUtils.register(new DateConvert(), java.util.Date.class);
ConvertUtils.register(new DateConvert(), java.sql.Date.class);
ConvertUtils.register(new DateConvert(), java.sql.Timestamp.class);
}

public static void copyProperties(Object dest, Object orig) {
try {
BeanUtils.copyProperties(dest, orig);
} catch (IllegalAccessException ex) {
ex.printStackTrace();
} catch (InvocationTargetException ex) {
ex.printStackTrace();
}
}
}



最后调用:
ExtBeanUtils.copyProperties(toObject, fromObject);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值