
3条回答
luoganlin 2016-12-06 03:14关注// String birth = "2000-08-05";
String birth = "2000-09-15";
String today = "2011-09-12";
SimpleDateFormat ageFormat = new SimpleDateFormat("yyyy-MM-dd");try { Date birthDayD = ageFormat.parse(birth); Date todayD = ageFormat.parse(today); int birthYear = birthDayD.getYear(); int birthMonth = birthDayD.getMonth(); int birthDay = birthDayD.getDay(); int todayYear = todayD.getYear(); int todayMonth = todayD.getMonth(); int todayDay = todayD.getDay(); int age = todayYear - birthYear; if(todayMonth - birthMonth < 0){ age = age - 1; } else if(todayMonth - birthMonth == 0){ if(todayDay - birthDay < 0){ age = age - 1; } } System.out.println("age=" + age); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); }解决 无用评论 打赏 举报