Mysql Date type field returning incorrect value in Java

Asked

Viewed 110 times

-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?

  • On the line contato.getDataNascimento() add getTime(), being like this: contato.getDataNascimento().getTime()

  • And what the right value should be?

  • @Gilvanandré Yes in this format dd/MM/YYYY

  • Thanks @adventistaa was that right!

  • @hkotsubo dd/MM/YYYY.

Show 1 more comment

1 answer

1


By taking advantage of your code in the getDtNascimento function, you can add getTime().

Example:

Calendar data = Calendar.getInstance();
data.setTime(rs.getDate("dataNascimento"));
contato.setDataNascimento(data); # Retorna a Data

And when it comes time to call you that:

System.out.println("Data de Nascimento: " + contato.getDataNascimento().getTime() );

If you want to format the date the Brazilian format can do the following before:

SimpleDateFormat formatoBR = new SimpleDateFormat( "dd/MM/yyyy" );
String newDate = formatoBR.format( contato.getDataNascimento().getTime() );
System.out.println("Data de Nascimento: " + newDate );
  • That’s what it was, it worked. Thank you very much!

Browser other questions tagged

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