|
|
@ -4,15 +4,17 @@ import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
import java.text.ParseException; |
|
|
|
import java.text.SimpleDateFormat; |
|
|
|
import java.time.LocalDate; |
|
|
|
import java.util.Calendar; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.GregorianCalendar; |
|
|
|
import java.util.Hashtable; |
|
|
|
import java.util.regex.Matcher; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
import static java.util.regex.Pattern.*; |
|
|
|
import static java.util.regex.Pattern.compile; |
|
|
|
|
|
|
|
/** |
|
|
|
* 身份证号校验 |
|
|
@ -24,6 +26,15 @@ public class IdCardNoValidatorUtils { |
|
|
|
|
|
|
|
private static Logger logger = LoggerFactory.getLogger(IdCardNoValidatorUtils.class); |
|
|
|
|
|
|
|
/** |
|
|
|
* 15位身份证号 |
|
|
|
*/ |
|
|
|
private static final Integer FIFTEEN_ID_CARD = 15; |
|
|
|
/** |
|
|
|
* 18位身份证号 |
|
|
|
*/ |
|
|
|
private static final Integer EIGHTEEN_ID_CARD = 18; |
|
|
|
|
|
|
|
/** |
|
|
|
* 身份证验证 |
|
|
|
* |
|
|
@ -216,15 +227,15 @@ public class IdCardNoValidatorUtils { |
|
|
|
int nowMonth = now.getMonthValue(); |
|
|
|
int cardYear = 0; |
|
|
|
int cardMonth = 0; |
|
|
|
if (StringUtils.isNotBlank(IDCard) && checkIsIdCardNo(IDCard)) { |
|
|
|
if (IDCard.length() == 15) { |
|
|
|
if (StringUtils.isNotBlank(IDCard) && isValid(IDCard)) { |
|
|
|
if (IDCard.length() == FIFTEEN_ID_CARD) { |
|
|
|
// 身份证上的年份(15位身份证为1980年前的)
|
|
|
|
String uyear = "19" + IDCard.substring(6, 8); |
|
|
|
cardYear = Integer.parseInt(uyear); |
|
|
|
// 身份证上的月份
|
|
|
|
String uyue = IDCard.substring(8, 10); |
|
|
|
cardMonth = Integer.parseInt(uyue); |
|
|
|
} else if (IDCard.length() == 18) { |
|
|
|
} else if (IDCard.length() == EIGHTEEN_ID_CARD) { |
|
|
|
// 身份证上的年份
|
|
|
|
String year = IDCard.substring(6).substring(0, 4); |
|
|
|
cardYear = Integer.parseInt(year); |
|
|
@ -243,5 +254,47 @@ public class IdCardNoValidatorUtils { |
|
|
|
return age; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 身份证验证是否有效 |
|
|
|
* |
|
|
|
* @param id 号码内容 |
|
|
|
* @return boolean |
|
|
|
* @author |
|
|
|
* @date |
|
|
|
*/ |
|
|
|
public static boolean isValid(String id) { |
|
|
|
Boolean validResult = true; |
|
|
|
//校验长度只能为15或18
|
|
|
|
int len = id.length(); |
|
|
|
if (len != FIFTEEN_ID_CARD && len != EIGHTEEN_ID_CARD) { |
|
|
|
validResult = false; |
|
|
|
} |
|
|
|
//校验生日
|
|
|
|
if (!validDate(id)) { |
|
|
|
validResult = false; |
|
|
|
} |
|
|
|
return validResult; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 校验生日 |
|
|
|
* |
|
|
|
* @param id |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
private static boolean validDate(String id) { |
|
|
|
try { |
|
|
|
String birth = id.length() == FIFTEEN_ID_CARD ? "19" + id.substring(6, 12) : id.substring(6, 14); |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); |
|
|
|
Date birthDate = sdf.parse(birth); |
|
|
|
if (!birth.equals(sdf.format(birthDate))) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
} catch (ParseException e) { |
|
|
|
return false; |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|