IDCardUtil.java
import android.text.TextUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
import java.text.DecimalFormat;public class IDCardUtil {
//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”);
private static String birthDay;
//<editor-fold desc="性别,年龄,生日">
//根据身份证号获取性别
public static String getSex(String IDCard) {
String sex = "";
if (!TextUtils.isEmpty(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;
}
//根据身份证号获取年龄
public static Integer getAge0(String IDCard) {
Integer age = 0;
Date date = new Date();
if (!TextUtils.isEmpty(IDCard)) {
if (IDCard.length() == FIFTEEN_ID_CARD) {//15位身份证号
String birthYear15 = "19" + IDCard.substring(6, 8);// 身份证上的年份(15位身份证为1980年前的)
String birthMonth15 = IDCard.substring(8, 10);// 身份证上的月份
String birthDay15 = IDCard.substring(10, 12);// 身份证上的日
String thisYear15 = format.format(date).substring(0, 4);//当前年份
String thisMonth15 = format.format(date).substring(5, 7);//当前月份
String toDay15 = format.format(date).substring(8, 10);//当前日
if (Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15) <= 0) {
return age = 0;
}
if (Integer.parseInt(birthMonth15) < Integer.parseInt(thisMonth15)) {//7<9,已过
age = Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15);
}
if (Integer.parseInt(birthMonth15) == Integer.parseInt(thisMonth15)) {//9=9,当前月
if (Integer.parseInt(birthDay15) < Integer.parseInt(toDay15)) {//生日小于当前日期,已过+1
age = Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15);
}
if (Integer.parseInt(birthDay15) == Integer.parseInt(toDay15)) {//生日等于当前日期,今天过+1
age = Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15);
}
if (Integer.parseInt(birthDay15) > Integer.parseInt(toDay15)) {//生日大于当前日期,未过
age = Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15) - 1;
}
}
if (Integer.parseInt(birthMonth15) > Integer.parseInt(thisMonth15)) {//9>7未过,当前用户还没过生日
age = Integer.parseInt(thisYear15) - Integer.parseInt(birthYear15) - 1;
}
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {//18位身份证号
String birthYear18 = IDCard.substring(6).substring(0, 4);//身份证上的年份
String birthMonth18 = IDCard.substring(10).substring(0, 2);//身份证上的月份
String birthDay18 = IDCard.substring(12).substring(0, 2);//身份证上的日
String thisYear18 = format.format(date).substring(0, 4);//当前年份
String thisMonth18 = format.format(date).substring(5, 7);//当前月份
String toDay18 = format.format(date).substring(8, 10);//当前日
if (Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18) <= 0) {
return age = 0;
}
if (Integer.parseInt(birthMonth18) < Integer.parseInt(thisMonth18)) {//7<9,已过,当前月份大于用户出身的月份表示已过生日
age = Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18);
}
if (Integer.parseInt(birthMonth18) == Integer.parseInt(thisMonth18)) {//9=9,当前月
if (Integer.parseInt(birthDay18) < Integer.parseInt(toDay18)) {//生日小于当前日期,已过+1
age = Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18);
}
if (Integer.parseInt(birthDay18) == Integer.parseInt(toDay18)) {//生日等于当前日期,今天过+1
age = Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18);
}
if (Integer.parseInt(birthDay18) > Integer.parseInt(toDay18)) {//生日大于当前日期,未过
age = Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18) - 1;
}
}
if (Integer.parseInt(birthMonth18) > Integer.parseInt(thisMonth18)) {//9>7未过,当前用户还没过生日
age = Integer.parseInt(thisYear18) - Integer.parseInt(birthYear18) - 1;
}
}
}
return age;
}
//获取出生日期 yyyy年MM月dd日
public static String getBirthday(String IDCard) {
String birthday = "";
String year = "";
String month = "";
String day = "";
if (!TextUtils.isEmpty(IDCard)) {
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD) {
// 身份证上的年份(15位身份证为1980年前的)
year = "19" + IDCard.substring(6, 8);
//身份证上的月份
month = IDCard.substring(8, 10);
//身份证上的日期
day = IDCard.substring(10, 12);
//18位身份证号
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
// 身份证上的年份
year = IDCard.substring(6).substring(0, 4);
// 身份证上的月份
month = IDCard.substring(10).substring(0, 2);
//身份证上的日期
day = IDCard.substring(12).substring(0, 2);
}
birthday = year + "年" + month + "月" + day + "日";
}
return birthday;
}
//身份证验证,id号码内容 是否有效
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;
return validResult;
}
if (!validDate(id)) {
validResult = false;
}
return validResult;
}
//校验生日
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;
}
public static int getAge(String IDCard) {
if (TextUtils.isEmpty(IDCard)) {
return 0;
}
if (IDCard.length() == FIFTEEN_ID_CARD) {//15位身份证号
birthDay = "19" + IDCard.substring(6, 12);// 身份证上的年份(15位身份证为1980年前的)
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {//18位身份证号
birthDay = IDCard.substring(6, 14);
}
String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String yearStr = time.split("-")[0];//以"-"为分割的第一个字符串,yyyy
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, 8);
int year = Integer.valueOf(yearStr);
int yearBirth = Integer.valueOf(yearBirthStr);
int month = Integer.valueOf(monthStr);
int monthBirth = Integer.valueOf(monthBirthStr);
int day = Integer.valueOf(dayStr);
int dayBirth = Integer.valueOf(dayBirthStr);
if (year - yearBirth <= 0) {
return 0;
}
int age = year - yearBirth;
if (month - monthBirth > 0) {
return age;
}
if (month - monthBirth < 0) {
return --age;//自减1
}
if (day - dayBirth >= 0) {
return age;
}
return --age;
}
public static int IdNOToAge(String IdNO) {
int leh = IdNO.length();
String dates = "";
if (leh == 18) {
int se = Integer.valueOf(IdNO.substring(leh - 1)) % 2;
dates = IdNO.substring(6, 10);
SimpleDateFormat df = new SimpleDateFormat("yyyy");
String year = df.format(new Date());
int u = Integer.parseInt(year) - Integer.parseInt(dates);
return u;
} else {
dates = IdNO.substring(6, 8);
return Integer.parseInt(dates);
}
}
//</editor-fold>
//<editor-fold desc="姓名脱敏">
public static String getDesensitizedName(String realname) {
String name = "";
String Name = "";
String month = "";
name = realname.substring(realname.length() - 1, realname.length());
Name = "**" + name;
return Name;
}
//姓名脱敏 * 杨* 易*** 迪*********
public static String getDesensitizedNameXingEnd(String name) {
String newname = "";
if (name.length() <= 1) {
//System.out.println("*");
newname = "*";
} else {
newname = name.replaceAll("([\\u4e00-\\u9fa5]{1})(.*)", "$1" + createAsterisk(name.length() - 1));
}
return newname;
}
public static final int ONE = 1;
public static final int TWO = 2;
//姓名脱敏 杨 杨* 易**玺 迪********提
public static String getDesensitizedNameXingCenter(String realName) {
if (realName == null) {
return null;
}
if (realName.length() == ONE) {
return realName;
} else if (realName.length() == TWO) {
return realName.substring(0, 1) + "*";
} else {
Integer length = realName.length();
StringBuffer middle = new StringBuffer();
for (int i = 0; i < realName.substring(1, length - 1).length(); i++) {
middle.append("*");
}
return realName.substring(0, 1) + middle + realName.substring(length - 1, length);
}
}
//姓名脱敏2 杨 *洋 ***玺 *********提
public static String getDesensitizedNameXingStart(String realName) {
if (realName == null) {
return null;
}
if (realName.length() == ONE) {
return realName;
} else if (realName.length() == TWO) {
return "*" + realName.substring(1, realName.length());
} else {
Integer length = realName.length();
StringBuffer middle = new StringBuffer();
for (int i = 0; i < realName.substring(0, length - 1).length(); i++) {
middle.append("*");
}
return /*realName.substring(0, 1) + */middle + realName.substring(length - 1, length);
}
}
//</editor-fold>
//<editor-fold desc="身份证脱敏">
//tvCardId.setText(cardid.replaceAll("(\\d{12}|\\d{18})(\\d{3})", "***************$2"));
public static String getDesensitizedCardIdXingCenter(String IDCard) {
String idCard = "";
String id = "";
if (!TextUtils.isEmpty(IDCard)) {
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD) {
id = IDCard.replaceAll("(\\d{4})\\d{7}(\\w{4})", "$1" + createAsterisk(7) + "$2");
// id = IDCard.substring(12, IDCard.length());
//18位身份证号
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
id = IDCard.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1" + createAsterisk(10) + "$2");
//id = IDCard.substring(15, IDCard.length());
}
idCard = id;
//idCard.replaceAll("(\\d{15})\\d{3}","***************$2");
}
return idCard;
}
public static String getDesensitizedCardIdXingEnd(String IDCard) {
String idCard = "";
String id = "";
if (!TextUtils.isEmpty(IDCard)) {
//15位身份证号
if (IDCard.length() == FIFTEEN_ID_CARD) {
id = IDCard.substring(12, IDCard.length());
//18位身份证号
} else if (IDCard.length() == EIGHTEEN_ID_CARD) {
id = IDCard.substring(15, IDCard.length());
}
idCard = "***************" + id;
//idCard.replaceAll("(\\d{15})\\d{3}","***************$2");
}
return idCard;
}
//生成很多个*号
public static String createAsterisk(int length) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < length; i++) {
stringBuffer.append("*");
}
return stringBuffer.toString();
}
//</editor-fold>
//<editor-fold desc="千分位方法">
//数字增加空格
public static String mainaaa(String args) {
Scanner sc = new Scanner(args);
StringBuilder str = new StringBuilder(sc.next());
sc.close();
//隔三个数字增加一个空格
for (int i = 3; i <= str.length() - 1; i += 4) {
str.insert(i, ' ');
}
return str.toString();
//System.out.println(str.toString());
}
//千分位
public static String fmtMicrometer(String text) {
DecimalFormat df = null;
if (text.indexOf(".") > 0) {
if (text.length() - text.indexOf(".") - 1 == 0) {
df = new DecimalFormat("###,##0.");
} else if (text.length() - text.indexOf(".") - 1 == 1) {
df = new DecimalFormat("###,##0.0");
} else {
df = new DecimalFormat("###,##0.0000");
}
} else {
df = new DecimalFormat("###,##0");
}
double number = 0.0;
try {
number = Double.parseDouble(text);
} catch (Exception e) {
number = 0.0;
}
String commaToSpace = df.format(number);//逗号替换成空格
commaToSpace = commaToSpace.replace(',', ' ');
return commaToSpace;
}
//</editor-fold>
}