1

There is a lot of similar topics but I couldn't find something similar then my problem. I've found only how to calculate if for example second time is greater then first time, like:

  1. 22:00
  2. 23:00

The result is easy to get. Just subtract second time with first using Date API. Difference is in milliseconds and you can easily convert them in seconds/minutes..

What I want to know, how to get difference between time in first day and time in second day, for example:

  1. 22:25
  2. 06:30

Difference should be 8 hours and 5 minutes.

Or another example

  1. 19:00
  2. 00:00

Difference should be 5 hours.

How to calculate time in this way? Any help is appreciated.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
Firefoxx Pwnass
  • 135
  • 1
  • 9
  • 2
    If you don't know the dates, the answer is ambiguous. But you could do the subtraction and then modulo-24 the answer... Thus if (a-b)<0, add 24. – Floris Apr 10 '13 at 13:03
  • Convert the 2 dates to milliseconds, subtract the 2 milliseconds and convert it back to date? Oh, you might want to do the abs() of 2 numbers if the value is less than zero. – Buhake Sindi Apr 10 '13 at 13:05
  • Do you always know that the second time is the next day? Or could they be the same day. – Floris Apr 10 '13 at 13:05
  • have a look in to this http://stackoverflow.com/a/5132698/1329126 – Sankar V Apr 10 '13 at 13:05
  • @Sankar V, I think the OP wants it in hours/minutes/seconds and not days. – Buhake Sindi Apr 10 '13 at 13:06
  • @BuhakeSindi just need to modify the last line in that code to convert it to hours/minutes/seconds whatever. Am I right? – Sankar V Apr 10 '13 at 13:11
  • @Sankar V, for each time unit, you will have to do your calculation (so a repeated calculation from the last line of code). – Buhake Sindi Apr 10 '13 at 15:47

2 Answers2

1

Convert your date and time to TimeStamp (It's just the long representation of a date in milliseconds since Jan. 1, 1970.), then calculate difference, and transferred from milliseconds to hours.

Also check this

Community
  • 1
  • 1
jimpanzer
  • 3,470
  • 4
  • 44
  • 84
0

Use Calendar class. Set your day, hour and minutes and finally subtract those dates (in milliseconds) and you will convert result in hour, minutes and seconds.

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196