Date
is a type for a time instant(時刻), not a time interval(時間).
So, if you want to caculate the time difference(time inverval) between two time instants(Date objects), you will get the time difference in another type, such as long
(in seconds) or type in the external library.
Refer to http://www.mkyong.com/java/how-to-calculate-date-time-difference-in-java/
The first code does not use an external library, but you have to convert milliseconds into seconds, minutes, hours, and so on.
Here is the important part:
Date d1 = ...;
Date d2 = ...;
//in milliseconds
long diff = d2.getTime() - d1.getTime();
long diffSeconds = diff / 1000 % 60;
long diffMinutes = diff / (60 * 1000) % 60;
long diffHours = diff / (60 * 60 * 1000) % 24;
long diffDays = diff / (24 * 60 * 60 * 1000);
System.out.print(diffDays + " days, ");
System.out.print(diffHours + " hours, ");
System.out.print(diffMinutes + " minutes, ");
System.out.print(diffSeconds + " seconds.");
The second code does use an external library, Joda-Time, which is one of the most famous Java library for Time.
Here is the important part:
DateTime dt1 = new DateTime(d1);
DateTime dt2 = new DateTime(d2);
System.out.print(Days.daysBetween(dt1, dt2).getDays() + " days, ");
System.out.print(Hours.hoursBetween(dt1, dt2).getHours() % 24 + " hours, ");
System.out.print(Minutes.minutesBetween(dt1, dt2).getMinutes() % 60 + " minutes, ");
System.out.print(Seconds.secondsBetween(dt1, dt2).getSeconds() % 60 + " seconds.");
If you want to use Joda-Time library, you can also use another type for time invervals, Interval
: http://joda-time.sourceforge.net/key_interval.html
EDIT:
The
Date
class uses internal long variable to represent the time instant.
And the long value is the time difference in milliseconds between some time instant,
namely January 1, 1970, 00:00:00 GMT
(which is specified in http://docs.oracle.com/javase/7/docs/api/java/util/Date.html),
which is January 1, 1970, 09:00:00
in Japan Standard Time because JST (UTC +09:00).
So, 23:00:05 - 08:00:01 will be 15:00:04, and will be converted into Japan Standard Time (UTC +09:00) = 24:00:04 (January 1, 1970), i.e 00:00:04 (January 2, 1970).
If you calculate 08:00:01 - 08:00:01, then it will be 1970-01-01 00:00:00 in UTC, which is 1970-01-01 09:00:00 in the Japan Standard time.
EDIT2: I made some mistake. I didn't use the Japan Standard Time when parsing. Anyway, the result is the same.
date1 = 08:00:01 in Japan = 23:00:01 in UTC
date2 = 23:00:05 in Japan = 14:00:05 in UTC
date = 14:00:05 in UTC - 23:00:01 in UTC = 15:00:04 in UTC
= 23:00:05 in Japan - 08:00:01 in Japan = 15:00:04 in UTC (NOT in JAPAN)
Why? Because the calculation is done with the long value I mentioned earlier which is calculated in GMT (UTC +00:00, or just UTC).
The long value is in UTC even if you specified you are using Japan time. Date
stores it converted to UTC, so it will get the time you gave -09:00
, and the internal process in Date
is done with UTC. If you want to print it, Date
just add +09:00
calculation.
You can also see it as:
23:00:05 with (+09:00) - 08:00:01 with (+09:00) = 15:00:04 with (+00:00)
anyway, 15:00:04 UTC
is 00:00:04 JST
, so if you print it, you will see the Japan Time 00:00:04
since you specified you are using Japan Standard Time.