Jsf 2 在Jsf中计算从出生日期算起的年龄(使用日历)

Jsf 2 在Jsf中计算从出生日期算起的年龄(使用日历),jsf-2,primefaces,Jsf 2,Primefaces,我有一个表单,我正在使用pimefaces日历组件,现在当用户使用该组件选择他的出生日期时,我想计算他当前的年龄,并需要在文本框中输入。年龄文本框应自动显示,因为用户将选择其出生日期 <h:outputLabel for="dobirth" value="Date of Birth (mu)" /> <p:calendar value="#{patient.dob}" id="popupCal"> <p:ajax event="dat

我有一个表单,我正在使用pimefaces日历组件,现在当用户使用该组件选择他的出生日期时,我想计算他当前的年龄,并需要在文本框中输入。年龄文本框应自动显示,因为用户将选择其出生日期

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

我正试图用这种方法计算年龄

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);
    }
public void handleDateSelect(DateSelectEvent事件)引发异常{
System.out.println(“内部获取年龄”);
FacesContext FacesContext=FacesContext.getCurrentInstance();
SimpleDateFormat=新的SimpleDateFormat(“d/M/yyyy”);
facesContext.addMessage(空,
新建FacesMessage(FacesMessage.SEVERITY_信息,“选定日期”,
format.format(event.getDate());
字符串dd=null;
dd=format.format(event.getDate());
系统输出打印项次(“日期dd”+dd);
日期日期=新的SimpleDataFormat(“MMMM d,yyyy”,Locale.ENGLISH).parse(dd);
System.out.println(“日期++”+日期);
日历出生=新的格里高利安日历();
今天日历=新的公历日历();
int calculatedAge=0;
整数因子=0;
日期currentDate=新日期();//当前日期
系统输出打印项次(“DOB”+DOB);
出生。设定时间(日期);
System.out.println(“设置出生”+出生);
今天。设置时间(当前日期);
如果(今天.get(日历年中的日期)
我还需要在用户使用日历选择出生时立即显示年龄

一旦我到了年龄,我如何在JSF2.0中显示它呢?

类似的东西(将getter添加到
calculatedAge
属性中)


类似的内容(将getter添加到
calculatedAge
属性)



如果表单ID已经在同一表单中,那么在
update
中前缀表单ID是不必要的,因此
prependId=“false”
在这里没有任何价值。这没关系,但我的意思是,您建议在不使用
prependId=“false”时使用
update=“:formID:age”
是错误的。@BalusC实际上我指的是,如果update=“age”找不到元素,他需要检查age id是否由某个容器预先提供……如果表单id已经在同一个表单中,那么在
update
中预先提供表单id是不必要的,因此
prependId=“false”
在这里没有任何价值。这没关系,但我的意思是,你建议在不使用
prependId=“false”
时使用
update=“:formID:age”
是错误的。@BalusC实际上我的意思是,如果update=“age”找不到元素,他需要检查年龄id是否由某个容器预先提供。。。
<h:form>
    <h:outputLabel for="dobirth" value="Date of Birth (mu)" />
    <p:calendar value="#{patient.dob}" id="popupCal">
        <p:ajax event="dateSelect" listener="#{patient.handleDateSelect}"      
               update="age" />
        </p:calendar>
    <h:outputText id="age" value="#{patient.calculatedAge}"/>
</h:form>