fork download
  1. import java.util.Date;
  2. import java.text.DateFormat;
  3. import java.text.SimpleDateFormat;
  4. import java.text.ParseException;
  5. import java.util.Calendar;
  6. import java.util.concurrent.TimeUnit;
  7.  
  8. class Ideone
  9. {
  10.  
  11. public static void main(String[] args)
  12. {
  13. try {
  14. System.out.print( diferencaHoras("02/01/2018 23:00", "03/01/2018 12:00") );
  15. } catch(Exception e) {
  16. System.out.println(e.getMessage());
  17. }
  18. }
  19.  
  20. public static int diferencaHoras(String h1, String h2) throws ParseException{
  21.  
  22. /* Alteração */
  23. DateFormat df = new SimpleDateFormat("dd/MM/yyyy HH:mm");
  24.  
  25. Date d1 = df.parse (h1);
  26. Date d2 = df.parse (h2);
  27.  
  28. long diff = d1.getTime() - d2.getTime();
  29.  
  30. int diffHours = (int) (diff / (60 * 60 * 1000));
  31.  
  32. return diffHours;
  33. }
  34. }
Success #stdin #stdout 0.07s 4575232KB
stdin
Standard input is empty
stdout
-13