Samantha
Samantha

Reputation: 167

Primefaces calendar - Not displaying the correct date

Hi am having an issue with prime-faces calendar. I am developing a Java web application, where I have a text box and a date picker from primefaces. The user will have to input his/her dob as string in the textbox. Then an ajax event is trigerred to populate the date picker automatically.

For example: The user has input 030589, the value of the date picker should be 03/05/1989. (Means that the latter was born on 3rd May 1989)

The problem that am facing is that: When user input 051132, the date picker value is 05/11/2032 instead of 05/11/1932. I try it with this value as well : 150435, the date picker value is 15/04/35 which is good. I cannot understand why it's happening only for 051132! Could you please help me with this?

Component:

   public void setValueDOB(){
       SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
       cur= emp.getDateInString();
       String day = cur.substring(0, 2);
       String month = cur.substring(2, 4);
       String year = cur.substring(4, 6);

       String dobTmp = day + "/" + month + "/" + year;

       dob = Calendar.getInstance();
       dob.setTime(format.parse(dobTmp));

       emp.setDob(dob);
    }

xhtml:

  <h:inputText id="dobTxt" value="Dob txt:"required="true" >
     <p:ajax update="dob" process="dobTxt" listener="#{Employee.setValueDOB()}" />
  </h:inputText>


 <h:outputText value="Title:" />

 <p:calendar disabled="false" pattern="dd/MM/yyyy" id="dob"
  value="#{EmployeeComponent.employee.dob}"
  required="true" converter="primefacesCalendarConverter" label="DOB"
  mindate="#{EmployeeComponent.dobMinDate}"maxdate="#{EmployeeComponent.dobMinDate}"                readonly="true">
 <f:validator validatorId="validatorForDOB" />

 </p:calendar>

Upvotes: 0

Views: 2217

Answers (3)

Anar Orujov
Anar Orujov

Reputation: 591

You can use

SimpleDateFormat.set2DigitYearStart(Date)

For Example:

SimpleDateFormat dateformat = new SimpleDateFormat("yy/MM/dd",Locale.US);

Calendar c = Calendar.getInstance(Locale.US);

c.set(1900, 0, 1);

dateformat.set2DigitYearStart(c.getTime());

System.out.println(dateformat.parse("05/11/32"));

Upvotes: 2

Olavi Mustanoja
Olavi Mustanoja

Reputation: 2065

The app has to convert a two-digit number into a year. This can be problematic now, since the year 50 can essentially mean 1950 or 2050. My suggestion is to reduce 100 from the year if it exceeds this year. For example:

if year is 38, read it as 2038. because 2038 > 2014 (this year), decrease the year by 100. The resulting year is 1938.

if year is 10, read it as 2010. because 2010 <= 2014, leave it be. The resulting year is therefore 2010.

The best way though would be to just get the year in dd/MM/yyyy format, as this cannot make mistakes.


The solution:

To fix your problem, do the following changes:

public void setValueDOB(){
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
    cur= emp.getDateInString();
    String day = cur.substring(0, 2);
    String month = cur.substring(2, 4);
    String year = cur.substring(4, 6);

    int currentYear = Calendar.YEAR; // not sure about this
    if (Integer.parseInt(year) > currentYear - 2000) {
        year = "19" + year;
    } else {
        year = "20" + year;
    }

    String dobTmp = day + "/" + month + "/" + year;

    dob = Calendar.getInstance();
    dob.setTime(format.parse(dobTmp));

    emp.setDob(dob);
}

Upvotes: 0

Thrax
Thrax

Reputation: 1964

It is caused by the default implementation of SimpleDateFormat. See : http://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

"For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some century. It does this by adjusting dates to be within 80 years before and 20 years after the time the SimpleDateFormat instance is created."

Here is how it goes in your case :

Now : 2014

First Date : 2032 -> Within +20 years

Second Date : 2035 -> Not within +20 years -> Substraction occurs

Upvotes: 2

Related Questions