|
@@ -5,7 +5,7 @@ import java.text.SimpleDateFormat;
|
|
|
import java.util.Calendar;
|
|
|
import java.util.Date;
|
|
|
|
|
|
-public class ValidateIdCardUtil {
|
|
|
+public class IdCardUtil {
|
|
|
|
|
|
// 每位加权因子
|
|
|
private static final int[] power = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
|
|
@@ -13,28 +13,28 @@ public class ValidateIdCardUtil {
|
|
|
/**
|
|
|
* 验证所有的身份证的合法性
|
|
|
*/
|
|
|
- public static boolean isValidatedIdcard(String idcard) {
|
|
|
- if (null == idcard || idcard.trim().equals("")) {
|
|
|
+ public static boolean isValidatedIdCard(String idCard) {
|
|
|
+ if (null == idCard || idCard.trim().equals("")) {
|
|
|
return false;
|
|
|
}
|
|
|
- if (idcard.length() == 15) {
|
|
|
- idcard = convertIdcarBy15bit(idcard);
|
|
|
+ if (idCard.length() == 15) {
|
|
|
+ idCard = convertIdcarBy15bit(idCard);
|
|
|
}
|
|
|
- if (null == idcard || idcard.trim().equals("")) {
|
|
|
+ if (null == idCard || idCard.trim().equals("")) {
|
|
|
return false;
|
|
|
}
|
|
|
- return isValidate18Idcard(idcard);
|
|
|
+ return isValidate18IdCard(idCard);
|
|
|
}
|
|
|
|
|
|
- private static boolean isValidate18Idcard(String idcard) {
|
|
|
+ private static boolean isValidate18IdCard(String idCard) {
|
|
|
// 非18位为假
|
|
|
- if (idcard.length() != 18) {
|
|
|
+ if (idCard.length() != 18) {
|
|
|
return false;
|
|
|
}
|
|
|
// 获取前17位
|
|
|
- String idcard17 = idcard.substring(0, 17);
|
|
|
+ String idcard17 = idCard.substring(0, 17);
|
|
|
// 获取第18位
|
|
|
- String idcard18Code = idcard.substring(17, 18);
|
|
|
+ String idcard18Code = idCard.substring(17, 18);
|
|
|
char[] c;
|
|
|
String checkCode;
|
|
|
// 是否都为数字
|
|
@@ -46,7 +46,7 @@ public class ValidateIdCardUtil {
|
|
|
|
|
|
int[] bit;
|
|
|
|
|
|
- bit = converCharToInt(c);
|
|
|
+ bit = convertCharToInt(c);
|
|
|
|
|
|
int sum17;
|
|
|
|
|
@@ -64,15 +64,15 @@ public class ValidateIdCardUtil {
|
|
|
/**
|
|
|
* 将15位的身份证转成18位身份证
|
|
|
*/
|
|
|
- private static String convertIdcarBy15bit(String idcard) {
|
|
|
+ private static String convertIdcarBy15bit(String idCard) {
|
|
|
String idcard17;
|
|
|
// 非15位身份证
|
|
|
- if (idcard.length() != 15) {
|
|
|
+ if (idCard.length() != 15) {
|
|
|
return null;
|
|
|
}
|
|
|
- if (isDigital(idcard)) {
|
|
|
+ if (isDigital(idCard)) {
|
|
|
// 获取出生年月日
|
|
|
- String birthday = idcard.substring(6, 12);
|
|
|
+ String birthday = idCard.substring(6, 12);
|
|
|
Date birthdate = null;
|
|
|
try {
|
|
|
birthdate = new SimpleDateFormat("yyMMdd").parse(birthday);
|
|
@@ -86,7 +86,7 @@ public class ValidateIdCardUtil {
|
|
|
cday.setTime(birthdate);
|
|
|
String year = String.valueOf(cday.get(Calendar.YEAR));
|
|
|
|
|
|
- idcard17 = idcard.substring(0, 6) + year + idcard.substring(8);
|
|
|
+ idcard17 = idCard.substring(0, 6) + year + idCard.substring(8);
|
|
|
|
|
|
char[] c = idcard17.toCharArray();
|
|
|
String checkCode;
|
|
@@ -94,7 +94,7 @@ public class ValidateIdCardUtil {
|
|
|
int[] bit;
|
|
|
|
|
|
// 将字符数组转为整型数组
|
|
|
- bit = converCharToInt(c);
|
|
|
+ bit = convertCharToInt(c);
|
|
|
int sum17;
|
|
|
sum17 = getPowerSum(bit);
|
|
|
|
|
@@ -186,7 +186,7 @@ public class ValidateIdCardUtil {
|
|
|
/**
|
|
|
* 将字符数组转为整型数组
|
|
|
*/
|
|
|
- private static int[] converCharToInt(char[] c) throws NumberFormatException {
|
|
|
+ private static int[] convertCharToInt(char[] c) throws NumberFormatException {
|
|
|
int[] a = new int[c.length];
|
|
|
int k = 0;
|
|
|
for (char temp : c) {
|
|
@@ -194,4 +194,46 @@ public class ValidateIdCardUtil {
|
|
|
}
|
|
|
return a;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据身份证号计算年龄
|
|
|
+ * */
|
|
|
+ public static int calAgeBySocialNo(String idCard) {
|
|
|
+ if (idCard.startsWith("K") || idCard.length() != 18) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ String birthDay = idCard.substring(6, 14);
|
|
|
+ String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
|
|
|
+ String yearStr = time.split("-")[0];
|
|
|
+ String monthStr = time.split("-")[1];
|
|
|
+ String dayStr = time.split("-")[2];
|
|
|
+ String yearBirthStr = birthDay.substring(0, 4);
|
|
|
+ String monthBirthStr = birthDay.substring(4, 6);
|
|
|
+ String dayBirthStr = birthDay.substring(6);
|
|
|
+ int year = Integer.parseInt(yearStr);
|
|
|
+ int yearBirth = Integer.parseInt(yearBirthStr);
|
|
|
+ if (year - yearBirth <= 0) {
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+ int age = year - yearBirth;
|
|
|
+ int month = Integer.parseInt(monthStr);
|
|
|
+ int monthBirth = Integer.parseInt(monthBirthStr);
|
|
|
+ if (month - monthBirth > 0) {
|
|
|
+ return age;
|
|
|
+ }
|
|
|
+ if (month - monthBirth < 0) {
|
|
|
+ return --age;
|
|
|
+ }
|
|
|
+ int day = Integer.parseInt(dayStr);
|
|
|
+ int dayBirth = Integer.parseInt(dayBirthStr);
|
|
|
+ if (day - dayBirth >= 0) {
|
|
|
+ return age;
|
|
|
+ }
|
|
|
+ return --age;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static int calSexByIdCard(String idCard) {
|
|
|
+ String gender = idCard.substring(16, 17);
|
|
|
+ return Integer.parseInt(gender) % 2 == 0 ? 2 : 1;
|
|
|
+ }
|
|
|
}
|