package com.util;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
/**
* 身份证号码工具
* @author Administrator
*
*/
public
class IdentityCardUtils{
/**
* 根据身份证号,取得其年龄
* @param idCard 身份证号码
* @return
* @throws Exception
*/
public static int getAge(String idCard) throws Exception{
String idCard18 = IdentityCardUtils.idCard15To18(idCard);
String birthday = idCard18.substring(6,14);
birthday = birthday.substring(0,4)+"-"+birthday.substring(4,6)+"-"+birthday.substring(4,8);
Date bd = IdentityCardUtils.string2Date(birthday);
return IdentityCardUtils.getBirthDayToAge(bd);
}
/**
* 将15位身份证号码转18位身份证号码
* @param id 15位身份证号码
* @return
*/
public static String idCard15To18(String idCard){
String Pid = "";
if(idCard.length()==15 && isN(idCard) && idCard!=null){
idCard = idCard.substring(0,6)+"19"+idCard.substring(6,15);
int a[] = new int[17];
for(int i=0;i<17;i++){
a[i] =
Integer.valueOf(idCard.charAt(i)+"").intValue();
}
int b[] = {7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2};
int c = 0;
int d = 0;
for(int i=0;i<17;i++){
c = a[i]*b[i];
d = d+c;
}
d = d%11;
String lastNumber = "";
switch (d) {
case 0 :
lastNumber="1";
break;
case 1 :
lastNumber="0";
break;
case 2 :
lastNumber="X";
break;
case 3 :
lastNumber="9";
break;
case 4 :
lastNumber="8";
break;
case 5 :
lastNumber="7";
break;
case 6 :
lastNumber="6";
break;
case 7 :
lastNumber="5";
break;
case 8 :
lastNumber="4";
break;
case 9 :
lastNumber="3";
break;
case 10 :
lastNumber="2";
break;
}
Pid = idCard+lastNumber;
}else{
Pid = idCard;
}
return Pid;
}
/**
* 将18位身份证号码,恢复为15位
* @param idCard 18位身份证号码
* @return
*/
public static String idCard18To15(String idCard){
if(idCard==null || idCard.length()!=18){
return idCard;
}
return idCard.substring(0,6)+idCard.substring(8,17);
}
http://www.javalearns.com/
/**
* 判断是否全是数字
* @param num
* @return
*/
public static boolean isN(String num){
boolean isNum = num.matches("\\d*");
return isNum;
}
/**
* 判断奇偶数
* @param n
* @return
*/
public static boolean IsOdd(int n){ return (n % 2 == 1) ? true : false;}
/**
* 判断是否为合法日期
* @param str_input
* @param rDateFormat
* @return
*/
public static boolean isDate(String str_input){
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
formatter.setLenient(false);
try {
formatter.format(formatter.parse(str_input));
} catch (Exception e) {
return false;
}
return true;
}
/**
* 根据生日得到年龄
* @param birthDay
* @return http://www.javalearns.com/
* @throws Exception
*/
public static int getBirthDayToAge(Date birthDay) throws Exception {
Calendar cal = Calendar.getInstance();
if (cal.before(birthDay)) {
throw new IllegalArgumentException(
"The birthDay is before Now.It's unbelievable!");
}
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH);
int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
cal.setTime(birthDay);
int yearBirth = cal.get(Calendar.YEAR);
int monthBirth = cal.get(Calendar.MONTH);
int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
int age = yearNow - yearBirth;
if (monthNow <= monthBirth) {
if (monthNow == monthBirth) {
//monthNow==monthBirth
if (dayOfMonthNow < dayOfMonthBirth) {
age--;
} else {
//do nothing
}
} else {
//monthNow>monthBirth
age--;
}
} else {
//monthNow<monthBirth
//donothing
}
return age;
}
/**
* 将yyyy-MM-dd 类型的字符串转为日期 *
* @param dateStr
* @return
*/
public static Date string2Date(String dateStr) {
if (dateStr != null && !"".equals(dateStr)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
return sdf.parse(dateStr);
} catch (Exception e) {
System.out.println(e);
}
}
return null;
}
public static void main(String[] args){
String id = "110108800127491";
String id18 = IdentityCardUtils.idCard15To18(id);
System.out.println(id18);
String id1 = IdentityCardUtils.idCard18To15(id18);
System.out.println(id1);
}
}
文章转载自 http://www.javalearns.com/Html/?1561.html
更多Java学习文章请访问 Java免费学习网 http://www.javalearns.com