Change format of a date from yyyy-MM-dd to dd-mm-yyyy

Asked

Viewed 11,756 times

5

I have a dataTable where I display some data but the date is coming in format yyyy-mm-dd how do I change the format of this date so that I can display it correctly in my dataTable? And where do I do this formatting? In Bean or DAO?

Here is my method that takes the database data and fills the dataTable:

@PostConstruct
    public void listar(){
        try{
            TarefaDAO tarefaDAO = new TarefaDAO();
            listaTarefa = tarefaDAO.listarPorUsuario(usuarioBean.getUsuarioLogado());   
        }catch(RuntimeException e){

        }
    }

DAO:

@SuppressWarnings("unchecked")
    public List<Tarefa> listarPorUsuario(Usuario usuario) {
        Session sessao = HibernateUtil.getSessionFactory().openSession();
        List<Tarefa> lista = null;
        try {
            Query consulta = sessao.getNamedQuery("Tarefa.listarPorCodigo");
            consulta.setParameter("usuario", usuario);
            lista = consulta.list();

        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            sessao.close();
        }
        System.out.println("LISTA NO DAO:" + lista);
        return lista;
    }

Declaration of variables that are dates in the Model:

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "data_inicio", nullable = false)
    private Date dataInicio;


    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "data_fim", nullable = false)
    private Date dataFim;

I want to display the date in the format dd-mm-yyyy

  • 2

    I think you’ve asked that question before.

3 answers

9

If you want another alternative it is possible to use the <f:convertDateTime/> within the outputText which is in the dataTable

Example:

<p:column headerText="Data">
    <h:outputText value="#{var.data}">
        <f:convertDateTime pattern="dd/MM/yyyy HH:mm"/>
    </h:outputText>
</p:column>

7


Use the Dateformat

SimpleDateFormat formatoDesejado = new SimpleDateFormat("dd/MM/yyyy");

String dataFormatada = null;

dataFormatada = formatoDesejado.format("sua data hora");

If you need to convert use .parse within the .format()

EDIT: inclusion of other examples to format @jsantos1991 Examples

EDIT: Convert String to Date using the template I mentioned above.

SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String dataString = dataFormatada //  <= sua data no formato de String; 

Date date = formatter.parse(dataString);
  • Other examples to format the date http://stackoverflow.com/a/24423756/3792998

  • Fine, I understand this however how I can format the value again to Date but in this formed dd-mm-yyyy, because my variable is of type date and it is in my list that fills the datatable

  • I can format the date to String already, but when I pass it to Date again it is like this: Tue Jul 14 09:22:00 BRT 2015

  • I edited there to convert to Date

  • Could you give an example of what goes on //passa sua data; ?

  • @Math, I put

Show 1 more comment

0

It worked for me.

minhaTextView.setText(new SimpleDateFormat("dd/MM/yyyy HH:mm")
             .format(meuObjeto.getDate()));

where "dd/MM/yyyy HH:mm" is the format I wish for and

"meuObjeto.getDate()" is the date I currently have.

Browser other questions tagged

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