Get the difference in hours of two DateTime Java Server Faces?

0

Good morning my query is the following in two Datetime I capture a start date and a final date, at the time of saving my final date in the database I need you to save me a data type Float with the difference of those two dates in hours I make a comparison but always ne yields the same result -1.

 public void botonTerminarTiempo() {

    tiempos.setFechaHoraFinal(fechaActual);
    tiemposFacade.edit(tiempos);
    Date fechaInicial = tiempos.getFechaHoraInicio();
    Date fechaFinal = tiempos.getFechaHoraFinal();
    Integer resultadoFecha;
    resultadoFecha = fechaInicial.compareTo(fechaFinal);
    tiempos.setTotalHoraHombre(resultadoFecha.floatValue());
    tiemposFacade.edit(tiempos);


    fechaActual = new Date();
    laborSeleccionada = new Labor();
    lineaSeleccionada = new Linea();
    operarioSeleccionado = new Operarios();
    ordentrabajo = new Ordentrabajo();
    tiempos = new Tiempos();
}
    
asked by Alexander Gil Tafur 08.09.2017 в 15:01
source

1 answer

1

The compareTo method you are using is not used to restrict dates, it is useful to compare them. This method returns -1, 0 or 1 depending on whether one date is greater than the other, equal or lesser.

If you want to find the difference between two dates, what you have to do is subtract them like this:

long diferenciaEn_ms = fechaFinal.getTime() – fechaInicial.getTime();

The difference will be obtained in a long type of variable, so if you need it in a Float you only have to convert it.

    
answered by 08.09.2017 / 15:14
source