Sum of hours as doubles

1

I have values that I have to add as hours, the problem is that these values reach me as double , where the first number is the hour and the second is the minutes, that is, for example:

  

valueA = 09.30 - > 9 hours and 30 minutes
  valueB = 01.45 - > 1 hour and 45 minutes
  totalValue = valueA + ValueB

I am using jodatime doing the following

 LocalDateTime totalHoras = new LocalDateTime()
                        .withHourOfDay(0)
                        .withMinuteOfHour(0)
                        .withSecondOfMinute(0);

    Double value1 = entry1.getValue();
                            Double value2 = partesTrabajoIdHorasLocal.get(key);
                            String value1String = new SimpleDateFormat("HH:mm").format(value1);
                            String value2String = new SimpleDateFormat("HH:mm").format(value2);


                            totalHoras = totalHoras.plusHours(obtenerHoras(String.valueOf(value1String)));
                            totalHoras = totalHoras.plusMinutes(obtenerMinutos(String.valueOf(value2String)));

                            totalHoras = totalHoras.plusHours(obtenerHoras(String.valueOf(value1String)));
                            totalHoras = totalHoras.plusMinutes(obtenerMinutos(String.valueOf(value2String)));

But I'm not adding hours. What am I doing wrong?

    
asked by JoCuTo 08.03.2018 в 12:41
source

1 answer

0

It is a bit strange the representation that is being used for minutes and hours, but hey, if that is the case here are my suggestions:

  • Do not use SimpleDateFormat, since time already includes formatters of time and date.
  • If you can use Java 8, use java.time which is a newer version of joda-time that is included with JDK 8.
  • Do not add the time values when they are in the form of Doubles, let the library take care of it.

I hope the following code will help you:

    Double tiempoInicial = 9.30;
    Double tiempoAdicional = 1.45;

    String[] tiempo = String.format("%2.2f", tiempoInicial).split("\.");
    int horas = Integer.valueOf(tiempo[0]);
    int minutos = Integer.valueOf(tiempo[1]);

    LocalDateTime totalHoras = new LocalDateTime()
            .withHourOfDay(horas)
            .withMinuteOfHour(minutos)
            .withSecondOfMinute(0)
            .withMillisOfSecond(0);
    System.out.printf("Hora inicial: %s\n", totalHoras.toString("HH:mm"));

    tiempo = String.format("%.2f", tiempoAdicional).split("\.");
    horas = Integer.valueOf(tiempo[0]);
    minutos = Integer.valueOf(tiempo[1]);

    LocalDateTime tiempoFinal = totalHoras
            .plusHours(horas)
            .plusMinutes(minutos);

    System.out.printf("Hora final: %s\n", tiempoFinal.toString("HH:mm"));

Final notes:

It would be ideal for the input to be separated in its hour and minute components or to come in the form of a properly formatted string so that the decimal point always exists. Taking into account those details you can use all or part of the conversions shown here.

Using doubles to represent time is not suitable because sometimes the numbers may lose accuracy like: 15 = > 14.9999999999. That is why it is necessary to adapt the input data to fit our program.

    
answered by 08.03.2018 / 17:07
source