-1
I am trying to read data from a Mysql table with Java, but the Date field is returning the following code:
java.util.GregorianCalendar[time=1558656000000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="GMT-03:00",offset=-10800000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=4,WEEK_OF_YEAR=21,WEEK_OF_MONTH=4,DAY_OF_MONTH=23,DAY_OF_YEAR=143,DAY_OF_WEEK=5,DAY_OF_WEEK_IN_MONTH=4,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=0,SECOND=0,MILLISECOND=0,ZONE_OFFSET=-10800000,DST_OFFSET=0]
This is the method that searches the bank’s values:
public List<Contato> getLista() {
try {
List<Contato> contatos = new ArrayList<Contato>();
PreparedStatement stmt = this.connection.prepareStatement("SELECT * FROM contatos");
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
Contato contato = new Contato();
contato.setId(rs.getLong("id"));
contato.setNome(rs.getString("nome"));
contato.setEmail(rs.getString("email"));
contato.setEndereco(rs.getString("endereco"));
Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("dataNascimento"));
contato.setDataNascimento(data); # Retorna a Data
contatos.add(contato);
}
rs.close();
stmt.close();
return contatos;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
And then I call in the Main method:
public static void main(String[] args) {
ContatoDao dao = new ContatoDao();
List<Contato> contatos = dao.getLista();
for (Contato contato : contatos) {
System.out.println("Nome: " + contato.getNome());
System.out.println("Email: " + contato.getEmail());
System.out.println("Endereço: " + contato.getEndereco());
System.out.println("Data de Nascimento: " + contato.getDataNascimento() + "\n");
}
}
To better understand, you would like to be able to display the formatted date, for example: 24/05/2019?
– Gilvan André
On the line
contato.getDataNascimento()
addgetTime()
, being like this:contato.getDataNascimento().getTime()
– adventistaam
And what the right value should be?
– hkotsubo
@Gilvanandré Yes in this format dd/MM/YYYY
– Devair Junior
Thanks @adventistaa was that right!
– Devair Junior
@hkotsubo dd/MM/YYYY.
– Devair Junior