-1

I want to calculate my age from date chooser and when i press the button my current age will appear..

how can I do that?

I can do this through this code:

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

But I want to make an input from date chooser as my age and I can calculate it by pressing a jbutton

  • Which Java Version you use? Java 8 provides a new Time Date Api. Here are some example of the API: [Java8](http://www.mscharhag.com/2014/02/java-8-datetime-api.html) – Patrick Aug 06 '14 at 07:16
  • add a bit more of your date chooser code cause that is the interesting bit – flup Aug 06 '14 at 07:16

1 Answers1

0

You can use Joda date/time library. For example:
import org.joda.time.{LocalTime, Years, LocalDate, Days}
...
Years.yearsBetween(client.birthDate, new LocalDate()).getYears();

Eugene Ustimenko
  • 322
  • 4
  • 16