1:将八位的年月日格式化成10位带横杠的年月日如:20140404->2014-04-04
public static Date formatSAP8Date(String sapDate) throws Exception {
boolean isInt = sapDate.matches("[0-9]*");
if (isInt == false)
throw new Exception("SAP日期格[" + sapDate + "]式校验错误,不符合8位数字类型日期格式。\n如:20140404");
String year = sapDate.substring(0, 4);
String month = sapDate.substring(4, 6);
String day = sapDate.substring(6, 8);
if (Integer.valueOf(month) > 12)
throw new Exception("月份[" + month + "]式校验错误,月份不能大于12个月.");
if (Integer.valueOf(day) > 31)
throw new Exception("日期[" + day + "]式校验错误,日期不能大于31天.");
return new SimpleDateFormat("yyyy-MM-dd").parse(year + "-" + month + "-" + day);
}
2:集合分割 可用于批量插入
public static List splitListByNum(List list, int num) {
if (list == null || list.size() == 0) {
return list;
}
if (num <= 0) {
new IllegalArgumentException("Wrong quantity.");
}
List wrapList = new ArrayList();
int count = 0;
while (count < list.size()) {
wrapList.add(new ArrayList(list.subList(count, (count + num) > list.size() ? list.size() : count + num)));
count += num;
}
return wrapList;
}
3:dto空校验
* @param stu 要检查的对象
* @param list 一个存放要检查元素的list 比如 NAME、AGE 注意:这里的元素对应DTO属性
* Collections.addAll添加元素使用这个方法
* @return
* 有空 true
* 无空 false
*/
public static boolean checkNull(Object stu, List list) {
// 全局变量控制最后结果
Boolean bb = false;
// 得到对象
Class c = stu.getClass();
// 得到所有的属性
Field[] ff = c.getDeclaredFields();
for (Field f : ff) {
f.setAccessible(true);
String ss = f.toString().substring(f.toString().lastIndexOf(".") + 1);
if (list.contains(ss)) {
//System.out.println("这是含有的属性:" + ss);
// String jutishuxing = (String)f.get(stu);
try {
PropertyDescriptor pd = new PropertyDescriptor(ss, c);
Method getMethod = pd.getReadMethod();// 获得get方法
if (pd != null) {
Object o = getMethod.invoke(stu);// 执行get方法返回一个Object
//System.out.println("这是得到的属性值:" + o);
bb = (o == null || "".equals(o));
}
if(bb){
return bb;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
return bb;
}
/***
*
* @param dto 要检查的dto对象
* @param keyList
* 一个存放要检查元素的list 比如 NAME、AGE 注意:这里的元素对应DTO属性
* Collections.addAll添加元素使用这个方法
* @param valueList
* 一个存放要检查元素的字段含义list 比如 对应上边的姓名、年龄 注意:这里的元素对应keyList
* @return
* 如果要检查的元素在dto中都不为空 返回null
* 如果含有空 返回示例:姓名、住址为空,请检查!
*
* 示例:
* Student stu = new Student("", "1", 123, "");NAME AGE phone home
ArrayList<String> keyList = new ArrayList<String>();
Collections.addAll(keyList, "NAME", "phone", "home");
ArrayList<String> valueList = new ArrayList<String>();
Collections.addAll(valueList, "姓名", "电话", "住址");
String bb = checkNullEnd(stu, keyList, valueList);
System.out.println("最后的判断结果~~~:" + bb);
*/
public static String checkNullAndGetNullColumn(Object dto, ArrayList<String> keyList, ArrayList<String> valueList) {
// 记录哪些属性值为空
ArrayList<String> errorList = new ArrayList<String>();
// 得到对象
Class<?> c = dto.getClass();
// 得到所有的属性
Field[] ff = c.getDeclaredFields();
for (int x = 0; x < ff.length; x++) {
ff[x].setAccessible(true);
String str = ff[x].toString();
String ss = str.substring(str.lastIndexOf(".") + 1);
for (int y = 0; y < keyList.size(); y++) {
if (keyList.get(y).equals(ss)) {
try {
PropertyDescriptor pd = new PropertyDescriptor(ss, c);
if (pd != null) {
Method getMethod = pd.getReadMethod();// 获得get方法
Object o = getMethod.invoke(dto);// 执行get方法返回一个Object
if (o == null || "".equals(o)) {
errorList.add(valueList.get(y));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
StringBuffer buffer = new StringBuffer();
if (errorList.isEmpty()) {
return null;
} else {
for (String errorColumn : errorList) {
buffer.append(errorColumn).append("、");
}
}
String str = buffer.append("为空,请检查!").toString();
int index = str.lastIndexOf("、");
return str.substring(0, index) + str.substring(index + 1);
}
未完待续,以后 写了再更新