下面一段代碼給大家分享java根據(jù)身份證計算年齡的方法,具體代碼如下所示:

birthDate = idCard.substring(6,10)+"-"+idCard.substring(10,12)+"-"+idCard.substring(12,14)
public static int getAgefromBirthTime(String birthTimeString){
// 先截取到字符串中的年、月、日
String strs[] = birthTimeString.trim().split("-");
int selectYear = Integer.parseInt(strs[0]);
int selectMonth = Integer.parseInt(strs[1]);
int selectDay = Integer.parseInt(strs[2]);
// 得到當前時間的年、月、日
Calendar cal = Calendar.getInstance();
int yearNow = cal.get(Calendar.YEAR);
int monthNow = cal.get(Calendar.MONTH) + 1;
int dayNow = cal.get(Calendar.DATE);
// 用當前年月日減去生日年月日
int yearMinus = yearNow - selectYear;
int monthMinus = monthNow - selectMonth;
int dayMinus = dayNow - selectDay;
int age = yearMinus;
if (yearMinus < 0) {// 選了未來的年份
age = 0;
} else if (yearMinus == 0) {// 同年的,要么為1,要么為0
if (monthMinus < 0) {// 選了未來的月份
age = 0;
} else if (monthMinus == 0) {// 同月份的
if (dayMinus < 0) {// 選了未來的日期
age = 0;
} else if (dayMinus >= 0) {
age = 1;
}
} else if (monthMinus > 0) {
age = 1;
}
} else if (yearMinus > 0) {
if (monthMinus < 0) {// 當前月>生日月
} else if (monthMinus == 0) {// 同月份的,再根據(jù)日期計算年齡
if (dayMinus < 0) {
} else if (dayMinus >= 0) {
age = age + 1;
}
} else if (monthMinus > 0) {
age = age + 1;
}
}
return age;
}
當前標題:JAVA根據(jù)身份證計算年齡的實現(xiàn)代碼-創(chuàng)新互聯(lián)
地址分享:http://chinadenli.net/article10/cosido.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、自適應(yīng)網(wǎng)站、品牌網(wǎng)站設(shè)計、小程序開發(fā)、網(wǎng)站策劃、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)