-3

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.

Community
  • 1
  • 1
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • 4
    What is the problem you are having? – Lalit Poptani Jun 26 '12 at 07:46
  • What do you want the difference units to be? It's easy to do in milliseconds, however, the sum of the differences won't translate into anything meaningful. – dcow Jun 26 '12 at 08:17
  • I want difference between two Times and add all diffrences. – Hardik Joshi Jun 26 '12 at 08:47
  • But that's not a problem! What you wrote on your question is no question, it's the "problem" (let's call it that way) solved! You already got all differences. 1 2 and 3.... what do you want to do with them now? I down voted your question until you better explain yourself. – Nuno Gonçalves Jun 26 '12 at 09:30
  • Have 6 time values. time1,time2,..time6. Now i get difference between these like (time2-time1),(time4-time3),(time6-time5). And after difference i want to add all these three differences. – Hardik Joshi Jun 26 '12 at 09:33
  • Long ---->totalDiff<----- = dif1 + dif2 + dif3!!! What more do you want? – Nuno Gonçalves Jun 26 '12 at 09:43
  • Yes exactly what i need. But i need totalDIff in hours:min:sec format. – Hardik Joshi Jun 26 '12 at 09:46
  • Can i know why you give me down votes? This is my requirement so that i ask it. sorry for any mistack. – Hardik Joshi Jun 26 '12 at 09:48
  • hardik jushi, I down voted your question beacuse even after asking you exactly what you wanted you didn't answer or updated your question until your comment. Why didn't you post this on the question? "But i need totalDIff in hours:min:sec format" You never told people what you really wanted!!! Even now, anybody coming here won't understant it. – Nuno Gonçalves Jun 26 '12 at 18:31

3 Answers3

2
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);
CSharp
  • 1,573
  • 2
  • 14
  • 25
0

Is this what you want?

long totalTime = (date2.getTime()-date1.getTime() + date4.getTime()-date3.getTime() + date6.getTime()-date5.getTime());

Date totalDifDate = new Date(totalTimeMillis);
Nuno Gonçalves
  • 6,202
  • 7
  • 46
  • 66
0

Better use Calendar class and GregorianClaendar instead of Date class.

Date time1, time2, time3, time4;
[...]
GregorianCalendar difference12 = new GregorianCalendar(0,0,0,0,0);
difference12.set(GregorianCalendar.MILLISECNOD, time1.getTime() - time2.getTime());

GregorianCalendar difference34 = new GregorianCalendar(0,0,0,0,0);
difference12.add(GregorianCalendar.MILLISECNOD, time3.getTime() - time4.getTime());

Date diff12 = difference12.getGregorianChange();
Date diff34 = difference34.getGregorianChange();
GregorianCalendar sum = new GregorianCalendar(0,0,0,0,0);
sum.add(GregorianCalendar.MILLISECOND, diff12.getTime() - time34.getTime());