自用工具类

自用工具类

    • cloneObj:对象的拷贝
    • clearMapNullValue:去掉map中值为null或为空的键
    • compareTime:比较两个时间的大小

cloneObj:对象的拷贝

    /**
     * 对象的拷贝(浅拷贝、深拷贝),只能拷贝Map,List
     *
     * @param obj    obj
     * @param depth    false->浅拷贝 true ->深拷贝
     */
    public static Object cloneObj(Object obj, boolean depth) throws Exception {
        if (obj == null){
            throw new Exception();
        }
        Object destObj;
        if (obj instanceof Map) {
            Map map = (Map) obj;
            destObj = new HashMap(16);
            Map destMap = (Map) destObj;
            destMap.putAll(map);
            if (depth) {
                for (Iterator<Map.Entry> ite = map.entrySet().iterator(); ite.hasNext();) {
                    Map.Entry entry = ite.next();
                    Object obj1 = entry.getValue();
                    if (obj1 instanceof Map || obj1 instanceof List) {
                        destMap.put(entry.getKey(), cloneObj(obj1, depth));
                    }
                }
            }
        } else if (obj instanceof List) {
            destObj = new ArrayList();
            List list = (List) obj;
            List destList = (List) destObj;
            destList.addAll(list);
            if (depth) {
                for (int i = 0, size = list.size(); i < size; i++) {
                    Object obj1 = list.get(i);
                    if (obj1 instanceof Map || obj1 instanceof List) {
                        destList.remove(i);
                        destList.add(cloneObj(obj1, depth));
                    }
                }
            }
        } else {
            destObj = obj;
        }
        return destObj;
    }

clearMapNullValue:去掉map中值为null或为空的键

    /**
     * 去掉map中值为null或为空的键,如果map为null,返回一个空的map对象
     *
     * @param param map
     */
    public static Map clearMapNullValue(Map param) throws Exception{
        if (param == null){
            return new HashMap(16);
        }
        Map tmp = (Map) cloneObj(param, true);
        for (Iterator<Map.Entry> ite = tmp.entrySet().iterator(); ite.hasNext();) {
            Map.Entry entry = ite.next();
            Object obj = entry.getValue();
            if (ValidateUtil.isEmpty(obj)) {
                param.remove(entry.getKey());
            } else if (obj instanceof String) {
                if ("null".equals(obj)) {
                    param.remove(entry.getKey());
                }
            } else if (obj instanceof Map) {
                param.put(entry.getKey(), clearMapNullValue((Map) obj));
            }
        }
        return param;
    }

compareTime:比较两个时间的大小

    /**
     * 比较两个时间的大小(返回-1, 0, 1)
     * 如果第一个时间大于第二个时间,返回1,相等0,小于-1
     *
     * @param timeOne 时间1
     * @param timeTwo 时间2
     * @param timeFormat 格式化 如: yyyy-MM-dd HH:mm:ss
     * @return int
     */
    public static int compareTime(String timeOne,String timeTwo,String timeFormat) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat(timeFormat);
        Date d1 = format.parse(timeOne);
        Date d2 = format.parse(timeTwo);
        long t2 = d2.getTime();
        long t1 = d1.getTime();
        return t1 > t2 ? 1 : t1 == t2 ? 0 : -1;
    }

你可能感兴趣的:(一些code,code,java)