4 changed files with 23 additions and 202 deletions
@ -1,178 +0,0 @@ |
|||||
package com.elink.esua.epdc.common.token.util; |
|
||||
|
|
||||
import org.apache.commons.lang3.StringUtils; |
|
||||
|
|
||||
import java.text.ParseException; |
|
||||
import java.text.SimpleDateFormat; |
|
||||
import java.util.Calendar; |
|
||||
import java.util.Date; |
|
||||
|
|
||||
/** |
|
||||
* 用户工具类 |
|
||||
* |
|
||||
* @author wanggongfeng |
|
||||
* @Date 19-12-16 |
|
||||
*/ |
|
||||
public class IdentityNoAnalysisUtil { |
|
||||
/** |
|
||||
* 15位身份证号 |
|
||||
*/ |
|
||||
private static final Integer FIFTEEN_ID_CARD=15; |
|
||||
/** |
|
||||
* 18位身份证号 |
|
||||
*/ |
|
||||
private static final Integer EIGHTEEN_ID_CARD=18; |
|
||||
private static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); |
|
||||
|
|
||||
/** |
|
||||
* 从身份证号码中获取生日 |
|
||||
* @param idno |
|
||||
* @return null表示idno错误,未获取到生日 |
|
||||
*/ |
|
||||
public static Date getBirthDay(String idno){ |
|
||||
if(!isValid(idno)){ |
|
||||
return null; |
|
||||
} |
|
||||
return toBirthDay(idno.substring(6, 14)); |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 根据身份证号获取性别 |
|
||||
* @param IDCard |
|
||||
* @return |
|
||||
*/ |
|
||||
public static String getSex(String IDCard){ |
|
||||
String sex =""; |
|
||||
if (StringUtils.isNotBlank(IDCard)){ |
|
||||
//15位身份证号
|
|
||||
if (IDCard.length() == FIFTEEN_ID_CARD){ |
|
||||
if (Integer.parseInt(IDCard.substring(14, 15)) % 2 == 0) { |
|
||||
sex = "女"; |
|
||||
} else { |
|
||||
sex = "男"; |
|
||||
} |
|
||||
//18位身份证号
|
|
||||
}else if(IDCard.length() == EIGHTEEN_ID_CARD){ |
|
||||
// 判断性别
|
|
||||
if (Integer.parseInt(IDCard.substring(16).substring(0, 1)) % 2 == 0) { |
|
||||
sex = "女"; |
|
||||
} else { |
|
||||
sex = "男"; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return sex; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 根据身份证号获取年龄 |
|
||||
* @param IDCard |
|
||||
* @return |
|
||||
*/ |
|
||||
public static Integer getAge(String IDCard){ |
|
||||
Integer age = 0; |
|
||||
Date date = new Date(); |
|
||||
if (StringUtils.isNotBlank(IDCard)&& isValid(IDCard)){ |
|
||||
//15位身份证号
|
|
||||
if (IDCard.length() == FIFTEEN_ID_CARD){ |
|
||||
// 身份证上的年份(15位身份证为1980年前的)
|
|
||||
String uyear = "19" + IDCard.substring(6, 8); |
|
||||
// 身份证上的月份
|
|
||||
String uyue = IDCard.substring(8, 10); |
|
||||
// 当前年份
|
|
||||
String fyear = format.format(date).substring(0, 4); |
|
||||
// 当前月份
|
|
||||
String fyue = format.format(date).substring(5, 7); |
|
||||
if (Integer.parseInt(uyue) <= Integer.parseInt(fyue)) { |
|
||||
age = Integer.parseInt(fyear) - Integer.parseInt(uyear) + 1; |
|
||||
// 当前用户还没过生
|
|
||||
} else { |
|
||||
age = Integer.parseInt(fyear) - Integer.parseInt(uyear); |
|
||||
} |
|
||||
//18位身份证号
|
|
||||
}else if(IDCard.length() == EIGHTEEN_ID_CARD){ |
|
||||
// 身份证上的年份
|
|
||||
String year = IDCard.substring(6).substring(0, 4); |
|
||||
// 身份证上的月份
|
|
||||
String yue = IDCard.substring(10).substring(0, 2); |
|
||||
// 当前年份
|
|
||||
String fyear = format.format(date).substring(0, 4); |
|
||||
// 当前月份
|
|
||||
String fyue = format.format(date).substring(5, 7); |
|
||||
// 当前月份大于用户出身的月份表示已过生日
|
|
||||
if (Integer.parseInt(yue) <= Integer.parseInt(fyue)) { |
|
||||
age = Integer.parseInt(fyear) - Integer.parseInt(year) + 1; |
|
||||
// 当前用户还没过生日
|
|
||||
} else { |
|
||||
age = Integer.parseInt(fyear) - Integer.parseInt(year); |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
return age; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 身份证验证 |
|
||||
* @param id 号码内容 |
|
||||
* @return 是否有效 |
|
||||
*/ |
|
||||
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() == 15 ? "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; |
|
||||
} |
|
||||
|
|
||||
/** |
|
||||
* 转换成日期 |
|
||||
* @param birthday |
|
||||
* @return |
|
||||
*/ |
|
||||
private static Date toBirthDay(String birthday){ |
|
||||
try{ |
|
||||
Calendar calendar = Calendar.getInstance(); |
|
||||
calendar.set(Calendar.YEAR, Integer.parseInt(birthday.substring(0, 4))); |
|
||||
calendar.set(Calendar.MONTH, Integer.parseInt(birthday.substring(4, 6)) - 1);//月份从0开始,所以减1
|
|
||||
calendar.set(Calendar.DAY_OF_MONTH, Integer.parseInt(birthday.substring(6, 8))); |
|
||||
//以下设置意义不大
|
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0); |
|
||||
calendar.set(Calendar.MINUTE, 0); |
|
||||
calendar.set(Calendar.SECOND, 0); |
|
||||
calendar.set(Calendar.MILLISECOND, 0); |
|
||||
|
|
||||
return calendar.getTime(); |
|
||||
}catch (Exception e){ |
|
||||
return null; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
Loading…
Reference in new issue