// str:被替换的字符串
// overlay:替换后的内容
// int :替换的起始下标(0开始计数,包含)
// end:替换的结尾下标(不包含)
String phone="18835698188";
String overlay = StringUtils.overlay(phone, "****", 3, 7);
System.out.println(overlay);
String idCard="410527202103031316";
String idCard2 = StringUtils.overlay(idCard, "****", 6, 14);
System.out.println(idCard2);
/**
* 从身份证中获取出生日期
*/
public static String getBirthDayFromIDCard(String idno) {
Calendar cd = Calendar.getInstance();
if (idno.length() == 15) {
cd.set(Calendar.YEAR, Integer.valueOf("19" + idno.substring(6, 8))
.intValue());
cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(8, 10))
.intValue() - 1);
cd.set(Calendar.DAY_OF_MONTH,
Integer.valueOf(idno.substring(10, 12)).intValue());
} else if (idno.length() == 18) {
cd.set(Calendar.YEAR, Integer.valueOf(idno.substring(6, 10))
.intValue());
cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(10, 12))
.intValue() - 1);
cd.set(Calendar.DAY_OF_MONTH,
Integer.valueOf(idno.substring(12, 14)).intValue());
}
return DateUtils.dateToString(cd.getTime());
}
/**
* 计算年龄
*
* @param birthday 生日日期
* @param calcDate 要计算的日期点
* @return
*/
public static int calcAge(Date birthday, Date calcDate) {
int cYear = DateUtils.getYearOfDate(calcDate);
int cMonth = DateUtils.getMonthOfDate(calcDate);
int cDay = DateUtils.getDayOfDate(calcDate);
int bYear = DateUtils.getYearOfDate(birthday);
int bMonth = DateUtils.getMonthOfDate(birthday);
int bDay = DateUtils.getDayOfDate(birthday);
if (cMonth > bMonth || (cMonth == bMonth && cDay > bDay)) {
return cYear - bYear;
} else {
return cYear - 1 - bYear;
}
}
/**
* 从身份证中获取出生日期
*/
public static String getBirthDayFromIDCard(String idno) {
Calendar cd = Calendar.getInstance();
if (idno.length() == 15) {
cd.set(Calendar.YEAR, Integer.valueOf("19" + idno.substring(6, 8))
.intValue());
cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(8, 10))
.intValue() - 1);
cd.set(Calendar.DAY_OF_MONTH,
Integer.valueOf(idno.substring(10, 12)).intValue());
} else if (idno.length() == 18) {
cd.set(Calendar.YEAR, Integer.valueOf(idno.substring(6, 10))
.intValue());
cd.set(Calendar.MONTH, Integer.valueOf(idno.substring(10, 12))
.intValue() - 1);
cd.set(Calendar.DAY_OF_MONTH,
Integer.valueOf(idno.substring(12, 14)).intValue());
}
return DateUtils.dateToString(cd.getTime());
}