java根據身份證計算年齡


/** * 根據身份證計算年齡 * * @param idcard * @return */ public static Integer idCardToAge(String idcard) { Integer selectYear = Integer.valueOf(idcard.substring(6, 10)); //出生的年份 Integer selectMonth = Integer.valueOf(idcard.substring(10, 12)); //出生的月份 Integer selectDay = Integer.valueOf(idcard.substring(12, 14)); //出生的日期 Calendar cal = Calendar.getInstance(); Integer yearMinus = cal.get(Calendar.YEAR) - selectYear; Integer monthMinus = cal.get(Calendar.MONTH) + 1 - selectMonth; Integer dayMinus = cal.get(Calendar.DATE) - selectDay; Integer age = yearMinus; if (yearMinus < 0) { age = 0; } else if (yearMinus == 0) { age = 0; } else if (yearMinus > 0) { if (monthMinus == 0) { if (dayMinus < 0) { age = age - 1; } } else if (monthMinus > 0) { age = age + 1; } } return age; }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM