Format date in Java web with Primefaces

7

Speak, 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 entry 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 looks like this:

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

The entity like this:

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

And it shows like this:

I wanted it to show like this:

  

09/30/2016 - Day / Month / Year and without schedules.

    
asked by anonymous 23.09.2016 / 19:31

3 answers

4

Put a specific getter in your entity:

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

From there 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% w /% does not change. Only filterBy is changed.

    
23.09.2016 / 19:38
2

Using a date of type java.util.Date , to resolve only in 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 ) is also not affected and still understands the field as date, but the display with outputText will be formatted.

    
01.08.2018 / 19:02
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>
    
01.08.2018 / 19:12