解决办法参考:http://hi.baidu.com/fcp_bd/blog/item/0e632783c08836a50cf4d2c4.html/cmtid/53484428cab979f399250ad7
org.apache.commons.beanutils.ConversionException: No value specified for ‘Date’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
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 ""; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
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);