Error - JSF and Primefaces

Asked

Viewed 300 times

0

Error trying to register user, the following message appears:

accordion:dtNasc: '22-02-1990' could not be understood as a date.

XHTML:

<p:outputLabel class="lt" value="Data de Nascimento:" />
<p:calendar id="dtNasc" value="#{usuarioBean.funcionario.dt_nasc}" locale="pt" yearRange="-99:+39" widgetVar="dtNasc"navigator="true" showButtonPanel="true">
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>

Staff Class

@NotEmpty(message = "O campo data de nascimento é obrigatório.")
@Column(name = "fun_dt_nasc", nullable = false)
private String dt_nasc;


public String getDt_nasc() {
    return new SimpleDateFormat("dd/MM/yyyy").parse(getDt_nasc());
}
public void setDt_nasc(String dt_nasc) {
    setDt_nasc(new SimpleDateFormat("dd/MM/yyyy").format(dt_nasc));
}

In my get it gives a mistake: Type Mismatch: cannot Convert from Date to String

  • 1

    What is the type of the dt_nasc attribute in the working class?

  • It is String... private String dt_nasc;

  • Calendar requires the attribute to be of the Date type

1 answer

2

As you said, your dt_nasc attribute is of the String type and Calendar needs to be of the Date type.

Either you change this or create a method to convert, for example:

public Date getDtNasc(){
    return new SimpleDateFormat("dd/MM/yyyy").parse(getDt_nasc());
}
public void setDtNasc(Date data){
    setDt_nasc(new SimpleDateFormat("dd/MM/yyyy").format(data));
}

And in your xhtml:

<p:calendar id="dtNasc" value="#{usuarioBean.funcionario.dtNasc}" locale="pt" yearRange="-99:+39" widgetVar="dtNasc"navigator="true" showButtonPanel="true">
    <f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
  • Nilson, not being able to register changing the attribute to Date, and when I put the same you did in the get and set gives error... I will post in the code above....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.