I have a form in which I am using pimefaces calender component, now when user will select his date of birth using this component, I want to calculate his current age and need to put in a textbox. The age textbox should get automatically displayed as user will select his date of birth.
<h:outputLabel for="dobirth" value="Date of Birth (mu)" />
<p:calendar value="#{patient.dob}" id="popupCal">
<p:ajax event="dateSelect" listener="#{patient.handleDateSelect}" />
</p:calendar>
I am trying to calculate age by this way.
public void handleDateSelect(DateSelectEvent event) throws ParseException {
System.out.println("inside get age");
FacesContext facesContext = FacesContext.getCurrentInstance();
SimpleDateFormat format = new SimpleDateFormat("d/M/yyyy");
facesContext.addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected",
format.format(event.getDate())));
String dd=null;
dd=format.format(event.getDate());
System.out.println("date dd"+dd);
Date date = new SimpleDateFormat("MMMM d, yyyy", Locale.ENGLISH).parse(dd);
System.out.println("date+++++++++"+date);
Calendar birth = new GregorianCalendar();
Calendar today = new GregorianCalendar();
int calculatedAge = 0;
int factor = 0;
Date currentDate = new Date(); // current date
System.out.println("DOB" + dob);
birth.setTime(date);
System.out.println("set birth" + birth);
today.setTime(currentDate);
if (today.get(Calendar.DAY_OF_YEAR) < birth.get(Calendar.DAY_OF_YEAR)) {
factor = -1;
}
calculatedAge = today.get(Calendar.YEAR) - birth.get(Calendar.YEAR)
+ factor;
System.out.println("age is " + calculatedAge);
}
And I also need to display age as soon as user selects his birth using calender.
How can I display it in jsf 2.0 once I got my age.?
Something like this (add getter to
calculatedAgeproperty).