javajtextfieldnetbeans-8jdatechooser

select date from jdatechooser and automatically diplay age in a Textfield


i'm trying to get the selected date from Jdatechooser and display the age corresponding to the date in a Textfield, i saw all the threads discussing this problem but it doesn"t work for me.here is my code:

 private void datenaiss_choozPropertyChange(java.beans.PropertyChangeEvent evt) {                                               
    Date date = (Date) datenaiss_chooz.getDate(); 
LocalDate ld = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
Period p =Period.between(ld, LocalDate.now());
age.setText(p);


}              

Solution

  • finally after long hours i got the solution, thanks everybody for helping me.Here is my code that works perfectly :

    DateTime start = new DateTime(datenaiss_chooz.getDate());
    Years h = Years.yearsBetween(start, new DateTime()); 
    StringBuilder b = new StringBuilder();
    b.append(h.getYears());
    String str = b.toString();
    if ( h.getYears()==0)
    age.setText(""); 
    else
    age.setText(str+" "+"ans");