如何计算17/08/1992
和18/06/2013
( day/month/year
)之间的年龄
我想要结果日月年
这似乎工作
GregorianCalendar d1 = new GregorianCalendar(1992, 8 - 1, 17);
GregorianCalendar d2 = new GregorianCalendar(2013, 6 - 1, 18);
d2.add(Calendar.YEAR, -d1.get(Calendar.YEAR));
d2.add(Calendar.MONTH, -d1.get(Calendar.MONTH));
d2.add(Calendar.DAY_OF_MONTH, -d1.get(Calendar.DAY_OF_MONTH) + 1);
int y = d2.get(Calendar.YEAR);
int m = d2.get(Calendar.MONTH);
int d = d2.get(Calendar.DAY_OF_MONTH) - 1;
虽然我不应该回答这个问题,但在看到您对@Harry Joy 评论的回复后,我已经回答了这个问题:P
Date date1 = new SimpleDateFormat("dd/MM/yyyy").parse(dateOfBirth);
Date date2 = new SimpleDateFormat("dd/MM/yyyy").parse(currentDate);
cal1.setTime(date1);
cal2.setTime(date2);
if(cal2.get(Calendar.DAY_OF_YEAR) < cal1.get(Calendar.DAY_OF_YEAR)) {
factor = -1;
}
age = cal2.get(Calendar.YEAR) - cal1.get(Calendar.YEAR) + factor;
System.out.println("Your age is: "+age);