Search code examples
androiddate-difference

Difference between two times in android


I want to get difference between two times. I calculate this by using following.

using this

But i want to calculate three difference. For example i have 6 TIME as following.

time1, time2, time3, time4

And I want to calculate difference between two times like

long difference1=time2-time1;
long difference2=time4-time3;
long difference3=time6-time5;

Then i want addition of these three differences. like difference1+difference2+difference3.


Solution

  • long difference1 = date2.getTime()-date1.getTime();  
    long difference2 = date4.getTime()-date3.getTime();  
    long difference3 = date6.getTime()-date5.getTime();
    
    ling totalDifference = difference1 + difference2 + difference3;  
    

    this totalDifference is in milliseconds and you can convert it in Day:Hour:Min:Seconds by

    days = (int) (totalDifference / (1000 * 60 * 60 * 24));  
    hours = (int) ((totalDifference - (1000 * 60 * 60 * 24 * days)) / (1000 * 60 * 60));  
    min = (int) (totalDifference - (1000 * 60 * 60 * 24 * days) - (1000 * 60 * 60 * hours))/ (1000 * 60);