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);