Formatting date in Java web with Primefaces

Asked

Viewed 1,866 times

5

Speak guys, I need to format a date in Java.

I’m using Java web, Primefaces, MVC, TDD, JSF, Hibernate. I am an intern and I am doing a Project Manager project for my company.

My date input view looks like this:

<h:outputText value="Data de Início"/>
    <p:calendar  value="#{projetoBean.projetoCadastro.dataInicio}"  pattern="dd/MM/yyyy">
    </p:calendar>   

My view to show the date is like this:

        <p:column headerText="Data de Início" filterBy="#{projeto.dataInicio}">
    <h:outputText value="#{projeto.dataInicio}"/>
    </p:column> 

The entity so:

@Temporal(value = TemporalType.TIMESTAMP)
@Column(name = "dataInicio_projeto")
private Date dataInicio;

And she shows it like this:

view para mostrar a data

I wanted you to show it like this:

30/09/2016 - Day/Month/Year and no timetable.

3 answers

4


Place a specific getter in your entity:

public String getDataInicioFormatada() {
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
    sdf.setLenient(false);
    return sdf.format(this.getDataInicio());
}

So in your view you do this:

<p:column headerText="Data de Início" filterBy="#{projeto.dataInicio}">
    <h:outputText value="#{projeto.dataInicioFormatada}"/>
</p:column>

Note that the filterBy is not amended. Only the outputText is amended.

1

Test with the code below:

<p:column headerText="Data de Início" filterBy="#{projeto.dataInicio}">
    <h:outputText value="#{projeto.dataInicio}">
        <f:convertDateTime pattern="dd/MM/yyyy"/>
   </h:outputText>
</p:column>

0

Using a date of type java.util.Date, to solve only in the JSF, using <f:convertDateTime pattern="dd/MM/yyyy"/>.

Example:

<h:outputText value="#{lista.dtEmissao}">
    <f:convertDateTime pattern="dd/MM/yyyy"/>
</h:outputText>

Using your example:

<p:column headerText="Data de Início" filterBy="#{projeto.dataInicio}">
    <h:outputText value="#{projeto.dataInicio}">
        <f:convertDateTime pattern="dd/MM/yyyy"/>
    </h:outputText>
</p:column>

The filterBy (or sortBy) also not affected and still understands the field as date, but the display with the outputText will be formatted.

Browser other questions tagged

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