项目中刚遇到针对身份证号做出年龄判断对18岁以下理财用户的舍去方案,贴出来大家一起共享吧!!!
// 判断是否成年
public static boolean isAdult(String str){
//新版身份证十八位,老版身份证是十五位
if (isEmpty(str)||(15!=str.length()&&18!=str.length())) {
return false;
}else {
String birthday=str.substring(6, str.length()-4);
Calendar calendar=Calendar.getInstance();
calendar.set(Integer.parseInt(birthday.substring(0, 4))+18, Integer.parseInt(birthday.substring(4, 6))-1, Integer.parseInt(birthday.substring(6)),23,59);
return 0<=System.currentTimeMillis()-calendar.getTimeInMillis();
}
}