java工具类

忽略两个对象之间变量名称大小写不同(这里的对象是由String转成String和LocalDateTime类型)

 public <T> T transferObjectIgnoreCase(Object obj, Class clz) {
        T result = null;
        try {
            if (obj != null && !obj.equals("")) {
                result = (T) clz.newInstance();
                //获取目标类的属性集合
                Map<String, Field> destPropertyMap = new HashMap<>();
                for (Field curField : clz.getDeclaredFields()) {
                    destPropertyMap.put(curField.getName().toLowerCase(), curField);
                }
                //拷贝属性
                for (Field curField : obj.getClass().getDeclaredFields()) {
                    Field targetField = destPropertyMap.get(curField.getName().toLowerCase());
                    if (targetField != null) {
                        targetField.setAccessible(true);
                        curField.setAccessible(true);
                        if (targetField.getType() == String.class) {
                            targetField.set(result, curField.get(obj));
                        } else if (targetField.getType() == LocalDateTime.class) {
                            if(curField.get(obj)!=null){
                                targetField.set(result, DateUtil.setValueForTime(curField.get(obj).toString()));
                            }
                        }

                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

特殊的时间转化(字符串转LocalDateTime) 例如:String str = "5/2/2017 4:48:49 PM";

 /**
     * 修改时间类型
     *
     * @param time
     * @return
     */
    public static LocalDateTime setValueForTime(String time) {
        Date date = stringToDate(time);
        LocalDateTime localDateTime = LocalDateTimeUtils.parseDataToLocalDateTime(date);
        return localDateTime;
    }


    /**
     * 设置字符串转Date
     *
     * @param time
     * @return
     */
    public static Date stringToDate(String time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss z");;
        time = time.trim();
        if ((time.indexOf("/") > -1) && (time.indexOf("AM") > -1) || (time.indexOf("PM") > -1)) {
            formatter = new SimpleDateFormat("MM/dd/yyyy KK:mm:ss a", Locale.ENGLISH);
        }
        ParsePosition pos = new ParsePosition(0);
        java.util.Date ctime = formatter.parse(time, pos);
        return ctime;
    }
原文地址:https://www.cnblogs.com/snail-gao/p/12757888.html