-1

如何计算17/08/199218/06/2013( day/month/year)之间的年龄

我想要结果日月年

4

2 回答 2

3

这似乎工作

    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;
于 2013-02-28T06:59:46.617 回答
0

虽然我不应该回答这个问题,但在看到您对@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);
于 2013-02-28T06:27:09.877 回答