BeanUtils 实现字符串转换为java.util.Date类型

 oracle数据库中的Date类型是java.sql.date,所有在java中必须要是bean中的Date类型也为java.sql.Date.

我在用BeanUtils 转换 用request.getParameterMap() 获取的map集合时遇到了转换异常,

java.lang.IllegalArgumentException:

Caused by: java.lang.IllegalArgumentException: Cannot invoke com.ctcc.dt_telecom.bean.Task.setTask_begin_time on bean class 'class com.ctcc.dt_telecom.bean.Task' - argument type mismatch - had objects of type "java.lang.String" but expected signature "java.sql.Date"
	at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2181)
	at org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:2141)
	at org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1948)
	at org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:2054)
	at org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1015)
	at org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:830)
	at com.ctcc.dt_telecom.utils.WEBUtils.paramToBean(WEBUtils.java:30)
	at com.ctcc.dt_telecom.servlet.TaskServlet.saveTask(TaskServlet.java:56)
	... 28 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:2155)
	... 35 more

百度了好久发现了下面这个解决方案,发现了下面这个方案。

没有报错,但是Date类型的变量和其后面的变量都会为null   ,桑心

 

package com.ctcc.dt_telecom.utils;

import java.sql.Date;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.PropertyUtilsBean;

public class DateTimeConverter implements Converter{
	 private static final String DATE      = "yyyy-MM-dd";
	 private static final String DATETIME  = "yyyy-MM-dd HH:mm:ss";
	 private static final String TIMESTAMP = "yyyy-MM-dd HH:mm:ss.SSS";

	@Override
	public Object convert(Class type, Object value) {
		// TODO Auto-generated method stub
		 return toDate(type, value);
	}

	public static Object toDate(Class type, Object value) {
       if (value == null || "".equals(value))
           return null;
       if (value instanceof String) {
           String dateValue = value.toString().trim();
           int length = dateValue.length();
           if (type.equals(java.util.Date.class)) {
               try {
                   DateFormat formatter = null;
                   if (length <= 10) {
                       formatter = new SimpleDateFormat(DATE, new DateFormatSymbols(Locale.CHINA));
                       return formatter.parse(dateValue);
                   }
                   if (length <= 19) {
                       formatter = new SimpleDateFormat(DATETIME, new DateFormatSymbols(Locale.CHINA));
                       return formatter.parse(dateValue);
                   }
                   if (length <= 23) {
                       formatter = new SimpleDateFormat(TIMESTAMP, new DateFormatSymbols(Locale.CHINA));
                       return formatter.parse(dateValue);
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }
       return value;
   }
	
	public static <T>T transMap2Bean(HttpServletRequest request, T t) {  
		 Map<String, String[]> map = request.getParameterMap(); 
        try {  
        	DateTimeConverter dtConverter = new DateTimeConverter();
        	ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
        	convertUtilsBean.deregister(Date.class);
        	convertUtilsBean.register(dtConverter, Date.class);
        	BeanUtilsBean beanUtilsBean = new BeanUtilsBean(convertUtilsBean,
        		new PropertyUtilsBean());
        	beanUtilsBean.populate(t, map);
  
        } catch (Exception e) {  
           
        }  
  
        return t;  
  
    }  
}

原文地址

https://blog.csdn.net/lirui874125/article/details/46637195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值