1
Why is the method below returning -1? It should return the difference in hours between two dates.
public int diferencaHoras(String h1, String h2) throws ParseException{
DateFormat df = new SimpleDateFormat ("dd/MM/yyyy hh:mm");
Date d1 = df.parse (h1);
Date d2 = df.parse (h2);
long diff = d1.getTime() - d2.getTime();
int diffHours = (int) (diff / (60 * 60 * 1000) % 24);
return diffHours;
}
If I call the method with the parameters:
diferencaHoras("02/01/2018 23:00", "03/01/2018 12:00");
returns -1
You can turn the dates into timeInMillis subtract the largest by the smallest and then convert into hours, simpler way. = D
– Guilherme Maciel
Okay, thanks for the tip William.
– Jovani