Error bringing Time and Date

Asked

Viewed 236 times

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 one SELECT it does not bring the registered time in the bank but the current time.

Man select was 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 recorded two days ago is coming with the current date and time. Can someone give me a hand to solve this problem?

2 answers

1

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

Anyway, I suppose in the future you’ll need to calculate the time periods elapsed between the loan and the refund, right? In this case, you should be using a class like the Localdatetime, and not a string, to store this information: it already comes with a method minus (to calculate temporal differences).

  • Continues to bring the current date...

  • You can post the change you made to get*Pedido?

  • If you don’t want the current date, you have to stop pulling the current date - the function will simply be public String getDataPedido() { return this.dataPedido; }.

  • Damn, man, when I think I’m learning I get a kick in the shins... That’s what you said, gave it right... Thanks...

  • Then I needed to create two more methods... this one they:

0

So I needed 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();
}

Browser other questions tagged

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