Best way to calculate the difference between two times in java?

I have two times in an array, and I need to calculate the difference between them. I converted the hours to minutes, then added the remaining minutes. It gives me total minutes in general, as soon as I did it for both, I just minus one total minute from the other. Then they were converted back to hours and minutes.

  double no1 = Double.parseDouble(array[i][4]);

  int time1_calc = (int) (no1 * 100); //Remove decimal point

  int time1hours = (time1_calc) / 100;

  int time1mins = (time1_calc) % 100;

  int time1HM = time1hours*60;

  int time1_total = time1HM + time1mins;

The above code is used a second time, then I use:

 int total = time2_total - time1_total;

To do this, all calculations "look" at the work, but, for example, the difference between 10.18 and 09.35 is 1 hour 23 minutes or 83 minutes. It seems my program shows 43 minutes.

I tried other ways, but still can't get it to work, any ideas?

Thanks =)

+2
source share
6 answers
Calendar time1 =  Calendar.getInstance();
// assign time1 date from your array

Calendar time2 =  Calendar.getInstance();
// assign time2 date from your array

long diff = time1.getTimeInMills() - time2.getTimeInMills();
+1

DateFormat ( SimpleDateFormat) parseDouble() ( , ).

+4

Joda.

, .

+2
int time1mins = (time1_calc) % 100;

- , 100, 60. , ?

, Calendar.

+2

Date. getTime. (). . .

0
long timeMillis = System.currentTimeMillis();
      System.out.println("Current time in millis:"+timeMillis);   
      Thread.sleep(1200);     
      long timeMillis2 = System.currentTimeMillis();
      System.out.println("Current time in millis:"+timeMillis2);
      System.out.println("Difference betwwen 2 times:"+(timeMillis2-timeMillis));   
0

All Articles