Error fetching Time and Date

1

I am registering in the MySQL database values such as time and date.

Code JAVA :

public String getDataPedido() {

    StringBuilder horaSistema = new StringBuilder();

    GregorianCalendar pegarHora = new GregorianCalendar();
    horaSistema.append(pegarHora.get(GregorianCalendar.HOUR_OF_DAY));
    horaSistema.append(":");
    horaSistema.append(pegarHora.get(GregorianCalendar.MINUTE));
    horaSistema.append(":");
    horaSistema.append(pegarHora.get(GregorianCalendar.SECOND));
    return horaSistema.toString();
}

public void setDataPedido(String dataPedido) {
    this.dataPedido = dataPedido;
}

public String getHoraPedido() {
    StringBuilder mesString = new StringBuilder();

    GregorianCalendar pegarMes = new GregorianCalendar();
    mesString.append(pegarMes.get(GregorianCalendar.MONTH));
    mesString.append("/");
    mesString.append(pegarMes.get(GregorianCalendar.DAY_OF_MONTH));
    mesString.append("/");
    mesString.append(pegarMes.get(GregorianCalendar.YEAR));

    return mesString.toString();
}

public void setHoraPedido(String horaPedido) {
    this.horaPedido = horaPedido;
}

It inserts the time and date correctly, but when I give a SELECT it does not bring the time registered in the bank, but the current time.

My select looks like this:

public List<ClubeDoDvdPedidoEmprestimo> selicionarPedidosDeEmprestimo(ClubeDoDvdPedidoEmprestimo clube) throws SQLException{
    StringBuilder sql = new StringBuilder();
    sql.append("SELECT codemprestimo, dataemprestimo, horaemprestimo, codigo_socio_solicitou_emprestimo, dvd_codigo, socio_codigo ");
    sql.append("FROM solicitacaoemprestimo ");
    sql.append("WHERE socio_codigo = ? ");
    sql.append("ORDER BY dataemprestimo ASC, horaemprestimo ASC ");

    Connection conexao = FabricaDeConexao.conectar();

    PreparedStatement comando = conexao.prepareStatement(sql.toString());
    comando.setLong(1, clube.getSocio().getCodigo());

    ResultSet resultado = comando.executeQuery();

    List<ClubeDoDvdPedidoEmprestimo> retorno = new ArrayList<>();

    while(resultado.next()){

      final ClubeDoDvdPedidoEmprestimo cdpe = new ClubeDoDvdPedidoEmprestimo();
      cdpe.setCodEmprestimo((resultado.getLong("codemprestimo")));
      cdpe.setDataPedido(resultado.getString("dataemprestimo"));
      cdpe.setHoraPedido(resultado.getString("horaemprestimo"));

      cdpe.setCodigoSocioSolicitouEmprestimo(resultado.getLong("codigo_socio_solicitou_emprestimo"));

      retorno.add(cdpe);
    }
    return retorno;
 }

And so:

public void listarSocilitacoesEmprestimo(){
    AdicionarSocioVisao e = new AdicionarSocioVisao();
    e.solicitarCodigoUsuario();

    ClubeDoDvdPedidoEmprestimo find = new ClubeDoDvdPedidoEmprestimo();
    find.setSocio(e);


    try {
        SolicitarEmprestimoDAO dsdao = new SolicitarEmprestimoDAO();
        List<ClubeDoDvdPedidoEmprestimo> resultado = dsdao.selicionarPedidosDeEmprestimo(find);
        for(ClubeDoDvdPedidoEmprestimo d: resultado){
            System.out.println("Código Dvd: "+d.getCodEmprestimo());
            System.out.println("Data Pedido: "+d.getDataPedido());
            System.out.println("Hora Pedido: "+d.getHoraPedido());
            System.out.println("Código do sócio solicitou emprestimo: "+d.getCodigoSocioSolicitouEmprestimo());
            System.out.println("");
        }   
    } catch (SQLException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
    }
}

Only the time is coming current or whatever I registered two days ago is coming with the current date and time. Can someone give me a hand to solve this problem?

    
asked by anonymous 03.06.2015 / 17:09

2 answers

1

Its functions getDataPedido and getHoraPedido do not use the this.dataPedido and this.horaPedido fields; therefore, they can not return the value you recorded. These functions return the current time because GregorianCalendar works using current time .

Anyway, I suppose in the future you will need to calculate the time periods elapsed between the loan and the return, right? In this case, you should be using a class such as LocalDateTime , and not a string, to store this information: it already comes with a minus method (to calculate time differences).

    
03.06.2015 / 17:31
0

So I had to create two more methods, here they are, now it worked:

public String solicitarDataPedido(){
    LocalDateTime pegarData = LocalDateTime.now();
    LocalDate dataAtual = pegarData.toLocalDate();

    return dataPedido = dataAtual.toString();
}

public String solicitarHoraPedido(){
    LocalDateTime pegarData = LocalDateTime.now();
    LocalDate dataAtual = pegarData.toLocalDate();
    return horaPedido = dataAtual.toString();
}
    
03.06.2015 / 19:23