Display date correctly in a JTable

0

I'm not able to display the sql database date type Date in JTable, the date appears like this:

Iusethefollowingmethodtocallthetabledata:

publicArrayList<Alunos>read(){PreparedStatementstmt=null;ResultSetrs=null;ArrayList<Alunos>aluno=newArrayList();try{stmt=connection.prepareStatement("select * from alunos");
        rs = stmt.executeQuery();//guarda os resultados do select no rs

        while(rs.next()){ //enquanto o select gerar valor, o while será executado
            Alunos alu = new Alunos();

            //Date data =  (Date) alu.getDataNascimento().getTime();
            alu.setId(rs.getInt("id"));//nome da coluna onde será mostrado 
            alu.setNome(rs.getString("nome"));
            alu.setCpf(rs.getString("cpf"));
            alu.setRg(rs.getString("rg"));
            alu.setEndereco(rs.getString("endereco"));
            alu.setEstado(rs.getString("estado"));
            alu.setDataNascimento(toCalendar(rs.getDate("nasc")));
            aluno.add(alu);

        }
        stmt.close();
        rs.close();

    } catch (SQLException ex) {
        Logger.getLogger(AlunoDao.class.getName()).log(Level.SEVERE, null, ex);
    }
    return aluno;
}

public static Calendar toCalendar(Date date){ 
    GregorianCalendar cal = new GregorianCalendar();
    cal.setTime(date);
    return cal;
}

And here is where I call the method in JPanel :

public void readJTable(){//lê os dados para tabela
   DefaultTableModel modelo = (DefaultTableModel) jTAlunos.getModel();

   AlunoDao aluDao = new AlunoDao();

   for(Alunos a: aluDao.read()){///adiciona linha na tabela
       modelo.addRow(new Object[]{
           a.getId(),
           a.getNome(),
           a.getCpf(),
           a.getRg(),
           a.getEndereco(),
           a.getEstado(),
           a.getDataNascimento().toString()
       });
   }
}

This is the class where I save the data:

import java.util.Calendar;

public class Alunos {
private int id;
private String nome;
private String cpf;
private String rg;
private String endereco;
private String estado;
private Calendar dataNascimento;

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getNome() {
    return nome;
}

public void setNome(String nome) {
    this.nome = nome;
}

public String getCpf() {
    return cpf;
}

public void setCpf(String cpf) {
    this.cpf = cpf;
}

public String getRg() {
    return rg;
}

public void setRg(String rg) {
    this.rg = rg;
}

public String getEndereco() {
    return endereco;
}

public void setEndereco(String endereco) {
    this.endereco = endereco;
}

public String getEstado() {
    return estado;
}

public void setEstado(String estado) {
    this.estado = estado;
}

public Calendar getDataNascimento() {
    return dataNascimento;
}

public void setDataNascimento(Calendar dataNascimento) {
    this.dataNascimento = dataNascimento;
}

}

    
asked by anonymous 27.11.2018 / 20:42

2 answers

4

The ideal solution would be to use date class in class Alunos , since it does not make sense to use Calendar to store a "date of birth". I do not know if you have a good reason for this, but I recommend reviewing your modeling.

Using SimpleDateFormat you will not be able to format a Calendar type, as was suggested in the other response. Just a quick query in the method documentation SimpleDateFormat#format() " to see that it expects a type Date .

Even changing the type to Date , to prevent gambiarras in the view of JTable , you should create a renderer (examples here and

29.11.2018 / 02:20
-1

I think you just need to format the date, doing so:

 SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy");

 for(Alunos a: aluDao.read()){///adiciona linha na tabela
   modelo.addRow(new Object[]{
       a.getId(),
       a.getNome(),
       a.getCpf(),
       a.getRg(),
       a.getEndereco(),
       a.getEstado(),
       formatter.format(a.getDataNascimento())
   });
}

I'm not sure if it works with Calendar type.

    
27.11.2018 / 20:56