从身份证获取年龄
8:输入一个身份证号码(15位和18位)和一个年份,计算现在的年龄(忽略非法参数)
eg:610618199001020065 2011
输出:21
package prctice01; /*8. 输入一个身份证号码(15位和18位)和一个年份,计算现在的年龄(忽略非法参数) eg:610618199001020065 2011 输出:21*/ public class GetAgeFromIDCard { public static void main(String[] args) { String idCard = "610618199101020095"; int year = 2016; System.out.println("年龄是:"+GetAge(idCard,year)); } private static int GetAge(String id,int year) { String birth; if(id.length() == 15) { birth = id.substring(3, 7); } else if(id.length() == 18) birth = id.substring(6,10); else { System.out.println("输入的身份证号有误!"); return -1; } return year-(Integer.parseInt(birth)); } }