java - Primefaces 日历 - 未显示正确的日期

标签 java spring hibernate jsf primefaces

您好,我遇到了 prime-faces 日历问题。我正在开发一个 Java Web 应用程序,其中有一个文本框和一个来自 primefaces 的日期选择器。 用户必须在文本框中输入他/她的出生日期作为字符串。然后触发 ajax 事件以自动填充日期选择器。

例如:用户输入030589,则日期选择器的值应为03/05/1989。 (意味着后者出生于1989年5月3日)

我面临的问题是: 当用户输入 051132 时,日期选择器值为 05/11/2032,而不是 05/11/1932。我也尝试使用这个值:150435,日期选择器值是 15/04/35,这很好。 我不明白为什么它只发生在 051132 上!你能帮我解决这个问题吗?

组件:

   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>

最佳答案

这是由SimpleDateFormat的默认实现引起的。请参阅: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."

以下是您的情况:

Now : 2014

First Date : 2032 -> Within +20 years

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

关于java - Primefaces 日历 - 未显示正确的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26547522/

相关文章:

java - 是什么导致 "bad operand types for binary operator ' = =' "与第二种类型 <nulltype>?

java - 如何在 Spring 中处理 DataIntegrityViolationException?

java - 在 Spring 应用程序中使用 application.properties

java - IE 正在将 Controller 的响应作为文件

java - 我们可以将 Eclipse 内容辅助热键 `Ctrl+Space` 绑定(bind)到其他键吗

java - 在 Android 运行时请求位置权限

java - 创建可执行 jar 文件

java - 实体管理器-创建查询-嵌套选择

java - hibernate的 "property-ref"元素标签中的 "one-to-one"属性有什么作用?

java - 仅更新 hibernate 中允许的列