|
@ -0,0 +1,57 @@
|
|
|
|
package com.yihu.quota.etl.formula;
|
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by wxw on 2018/10/18.
|
|
|
|
* 根据身份证号码转换成相应的年龄段
|
|
|
|
* @author wxw.
|
|
|
|
*/
|
|
|
|
public class IdCard2AgeGroupFunc implements Functioner {
|
|
|
|
|
|
|
|
private AgeGroupFunc ageGroupFunc = new AgeGroupFunc();
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String execute(Object... args) throws Exception {
|
|
|
|
String Ai = "";
|
|
|
|
int age = 0;
|
|
|
|
String idCard = (String) args[0];
|
|
|
|
// 非身份证 直接返回6 其他
|
|
|
|
if (idCard.length() != 15 && idCard.length() != 18) {
|
|
|
|
return "6";
|
|
|
|
}
|
|
|
|
// 身份证号码截取 15、18位转成17位
|
|
|
|
if (idCard.length() == 18) {
|
|
|
|
Ai = idCard.substring(0, 17);
|
|
|
|
} else if (idCard.length() == 15) {
|
|
|
|
Ai = idCard.substring(0, 6) + "19" + idCard.substring(6, 15);
|
|
|
|
}
|
|
|
|
// 获取年月日
|
|
|
|
String strYear = Ai.substring(6, 10);// 年
|
|
|
|
String strMonth = Ai.substring(10, 12);// 月
|
|
|
|
String strDay = Ai.substring(12, 14);// 日
|
|
|
|
System.out.println(strYear + "-" + strMonth + "-" + strDay);
|
|
|
|
|
|
|
|
// 出生日期
|
|
|
|
Calendar birth = Calendar.getInstance();
|
|
|
|
birth.set(Calendar.YEAR, Integer.parseInt(strYear));
|
|
|
|
birth.set(Calendar.MONTH, Integer.parseInt(strMonth) - 1);
|
|
|
|
birth.set(Calendar.DAY_OF_MONTH, Integer.parseInt(strDay));
|
|
|
|
|
|
|
|
// 当前时间
|
|
|
|
Calendar now = Calendar.getInstance();
|
|
|
|
now.setTime(new Date());
|
|
|
|
|
|
|
|
// 计算年龄
|
|
|
|
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
|
|
|
|
int birthDay = now.get(Calendar.DAY_OF_YEAR);
|
|
|
|
int nowDay = birth.get(Calendar.DAY_OF_YEAR);
|
|
|
|
if (birthDay < nowDay) {
|
|
|
|
age -= 1;
|
|
|
|
}
|
|
|
|
// 调用年龄段方法
|
|
|
|
String ageGroup = ageGroupFunc.execute(age);
|
|
|
|
return ageGroup;
|
|
|
|
}
|
|
|
|
}
|