-1

我想从日期选择器计算我的年龄,当我按下按钮时,我的当前年龄将出现..

我怎样才能做到这一点?

我可以通过这段代码做到这一点:

Calendar cal = new GregorianCalendar(1999, 1, 1); //I WANT THIS AGE TO BE INPUT FROM DATESHOOSER)

Calendar now = new GregorianCalendar();
int res = now.get(Calendar.YEAR) - cal.get(Calendar.YEAR);
if ((cal.get(Calendar.MONTH) > now.get(Calendar.MONTH))
    || (cal.get(Calendar.MONTH) == now.get(Calendar.MONTH) && cal.get(Calendar.DAY_OF_MONTH) > now
        .get(Calendar.DAY_OF_MONTH))) {
  res--;
}
System.out.println(res);

jTextField3.setText(Integer.toString(res));  

但是我想从日期选择器中输入我的年龄,我可以通过按下 jbutton 来计算它

4

1 回答 1

0

您可以使用 Joda 日期/时间库。例如:
import org.joda.time.{LocalTime, Years, LocalDate, Days}
...
Years.yearsBetween(client.birthDate, new LocalDate()).getYears();

于 2014-08-06T08:06:12.953 回答