wuxw
2019-06-20 ad4aa3dea3554b168824e426e66bd9849e3193d4
java110-common/src/main/java/com/java110/common/util/BeanConvertUtil.java
@@ -2,9 +2,14 @@
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.PropertyUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Date;
import java.util.List;
import java.util.Map;
@@ -19,6 +24,33 @@
public final class BeanConvertUtil {
    private BeanConvertUtil() {
    }
    static {
        ConvertUtils.register(new Converter() { //注册一个日期转换器
            public Object convert(Class type, Object value) {
                Date date1 = null;
                if (value instanceof String) {
                    String date = (String) value;
                    SimpleDateFormat sdf = null;
                    if (date.contains(":")) {
                        sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    } else {
                        sdf = new SimpleDateFormat("yyyy-MM-dd");
                    }
                    try {
                        date1 = sdf.parse(date);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    return date1;
                }
                return value;
            }
        }, Date.class);
        ConvertUtils.register(new Java110StringConvert(), String.class);
    }
@@ -59,6 +91,7 @@
            returnModel = t.newInstance();
            BeanUtils.copyProperties(returnModel, orgBean);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("bean转换bean失败", e);
        }
        return returnModel;
@@ -89,11 +122,11 @@
     * @param orgBean 原始bean
     * @return map对象
     */
    public static  Map beanCovertMap(Object orgBean) {
    public static Map beanCovertMap(Object orgBean) {
        Map newMap = null;
        try {
            newMap = BeanUtils.describe(orgBean);
            newMap = PropertyUtils.describe(orgBean);
        } catch (Exception e) {
            throw new RuntimeException("bean转换Map失败", e);
        }