package com.cloud.user.utils;
|
|
import com.cloud.common.utils.DateUtil;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.Date;
|
|
|
public class PersonCotrollerUtil {
|
|
|
// 每位加权因子
|
private static int power[] = {7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2};
|
|
/**
|
* . 验证日期格式
|
*
|
* @param date 日期
|
*
|
* @return true false
|
*/
|
public static boolean isDate(String date) {
|
try {
|
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.DATE_FMT_3);
|
sdf.parse(date);
|
} catch (Exception e) {
|
return false;
|
}
|
return true;
|
|
|
}
|
|
/**
|
* . 根据身份证获得性别
|
*
|
* @param idCard 身份证号
|
*
|
* @return 男 1 女 2
|
*/
|
public static String getGender(String idCard) {
|
String genderNum = idCard.substring(idCard.length() - 2, idCard.length() - 1);
|
if (Integer.parseInt(genderNum) % 2 == 0) {
|
return "2";
|
} else {
|
return "1";
|
}
|
}
|
|
/**
|
* . 身份证验证
|
*
|
* @param idcard 身份证号
|
*
|
*/
|
public static boolean isCard2(String idcard){return true;};
|
public static boolean isCard(String idcard) {
|
// 非18位为假
|
if (idcard.length() != 18) {
|
return false;
|
}
|
// 获取前17位
|
String idcard17 = idcard.substring(0, 17);
|
// 获取第18位
|
String idcard18Code = idcard.substring(17, 18);
|
char c[] = null;
|
String checkCode = "";
|
// 是否都为数字
|
if (isDigital(idcard17)) {
|
c = idcard17.toCharArray();
|
} else {
|
return false;
|
}
|
|
if (null != c) {
|
int bit[] = new int[idcard17.length()];
|
|
bit = converCharToInt(c);
|
|
int sum17 = 0;
|
|
sum17 = getPowerSum(bit);
|
|
// 将和值与11取模得到余数进行校验码判断
|
checkCode = getCheckCodeBySum(sum17);
|
if (null == checkCode) {
|
return false;
|
}
|
// 将身份证的第18位与算出来的校码进行匹配,不相等就为假
|
if (!idcard18Code.equalsIgnoreCase(checkCode)) {
|
return false;
|
}
|
}
|
return true;
|
}
|
|
/**
|
* 数字验证
|
*
|
* @param str
|
* @return
|
*/
|
public static boolean isDigital(String str) {
|
return str == null || "".equals(str) ? false : str.matches("^[0-9]*$");
|
}
|
|
|
/**
|
* 将身份证的每位和对应位的加权因子相乘之后,再得到和值
|
*
|
* @param bit
|
* @return
|
*/
|
public static int getPowerSum(int[] bit) {
|
|
int sum = 0;
|
|
if (power.length != bit.length) {
|
return sum;
|
}
|
|
for (int i = 0; i < bit.length; i++) {
|
for (int j = 0; j < power.length; j++) {
|
if (i == j) {
|
sum = sum + bit[i] * power[j];
|
}
|
}
|
}
|
return sum;
|
}
|
|
/**
|
* 将和值与11取模得到余数进行校验码判断
|
* @param sum17
|
* @return 校验位
|
*/
|
public static String getCheckCodeBySum(int sum17) {
|
String checkCode = null;
|
switch (sum17 % 11) {
|
case 10:
|
checkCode = "2";
|
break;
|
case 9:
|
checkCode = "3";
|
break;
|
case 8:
|
checkCode = "4";
|
break;
|
case 7:
|
checkCode = "5";
|
break;
|
case 6:
|
checkCode = "6";
|
break;
|
case 5:
|
checkCode = "7";
|
break;
|
case 4:
|
checkCode = "8";
|
break;
|
case 3:
|
checkCode = "9";
|
break;
|
case 2:
|
checkCode = "x";
|
break;
|
case 1:
|
checkCode = "0";
|
break;
|
case 0:
|
checkCode = "1";
|
break;
|
}
|
return checkCode;
|
}
|
|
/**
|
* 将字符数组转为整型数组
|
*
|
* @param c
|
* @return
|
* @throws NumberFormatException
|
*/
|
public static int[] converCharToInt(char[] c) throws NumberFormatException {
|
int[] a = new int[c.length];
|
int k = 0;
|
for (char temp : c) {
|
a[k++] = Integer.parseInt(String.valueOf(temp));
|
}
|
return a;
|
}
|
|
|
/**
|
* . 身份证获取生日
|
*
|
* @param idCard 身份证号
|
*
|
*/
|
public static Date getBirthday(String idCard) {
|
String birthday =
|
idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
try {
|
return sdf.parse(birthday);
|
} catch (ParseException e) {
|
e.printStackTrace();
|
return null;
|
}
|
}
|
|
/**
|
* 手机验证
|
*
|
* @param mobile
|
* @return
|
*/
|
|
public static boolean isMobile(String mobile) {
|
/*String regexMoble = "^((17[0-9])|(14[0-9])|(13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
|
return Pattern.matches(regexMoble, mobile);*/
|
return true;
|
}
|
|
/**
|
* 电话座机验证
|
*
|
* @param phone
|
* @return
|
*/
|
|
public static boolean isPhone(String phone) {
|
/* String regexPhone = "^(0\\d{2}-\\d{8}(-\\d{1,4})?)|(0\\d{3}-\\d{7,8}(-\\d{1,4})?)$";
|
return Pattern.matches(regexPhone, phone);*/
|
return true;
|
}
|
|
|
|
}
|