// 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();
}
public static void main(String []args){
// String birth = "2000-08-05";
String birth = "2000-09-15";
test(birth);
}
public int test(birth){
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);
return age;
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}