cloneObj:对象的拷贝
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或为空的键
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:比较两个时间的大小
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;
}