2012-05-12 37 views
0

私は、pimefacesカレンダーコンポーネントを使用しているフォームを使用しています。ユーザーがこのコンポーネントを使用して生年月日を選択すると、現在の年齢と必要性を計算しますテキストボックスに入れる。ユーザーが生年月日を選択すると、年齢のテキストボックスが自動的に表示されます。生年月日から年齢を計算する(カレンダーを使用する)IN Jsf

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

また、ユーザーがカレンダーを使用して出生を選択するとすぐに年齢を表示する必要があります。

年齢を取得したらどのようにjsf 2.0で表示できますか?

答えて

3

このようなもの(ゲッターをcalculatedAgeプロパティに追加)。

+0

'prependId =" false "'が同じ値を持たないように、既に同じフォームの中にある場合は、 'update'でフォームIDをプレフィックスにする必要はありません。 – BalusC

+0

@BalusCああ、ちょうど私の習慣... :)編集... – Daniel

+0

それは問題ではありませんが、 'prependId ="を使用しない場合は 'update =":formID:age "偽 "は間違っています。 – BalusC

関連する問題