The answer by ADTC is correct.
Rolling your own date-time calculations is risky business. Time zones, Daylight Savings Time (DST), and other anomalies make such attempts failure-prone.
And rolling your own is silly since we have well-written and debugged code at our disposal. Currently we have the Joda-Time library, and with Java 8 we'll have the new java.time.* classes (inspired by Joda-Time). Joda-Time has classes built just for the purpose you are trying to jerry-rig, handling a span of time.
The following is copied from an answer I posted recently to a similar question, showing how to properly define a span of time and render a string describing that span.
The Joda-Time 2.3 library makes this kind of work easier. Look at the Period, Duration, and Interval classes.
In contrast to a java.util.Date, in Joda-Time a DateTime
instance does indeed know its assigned time zone.
The ISO 8601 standard defines a way to describe durations as hours, minutes, and such in a PnYnMnDTnHnMnS
format. I use that in my example code below. Joda-Time offers other ways as well.
// © 2013 Basil Bourque. This source code may be used freely forever by anyone taking full responsibility for doing so.
// import org.joda.time.*;
// import org.joda.time.format.*;
DateTimeZone timeZone = DateTimeZone.forID( "Europe/London" );
DateTime dateTimeNew = new DateTime( timeZone );
DateTime dateTimeOld = dateTimeNew.minusHours( 2 );
Period period = new Period( dateTimeOld, dateTimeNew );
Dump to console…
System.out.println( "dateTimeNew: " + dateTimeNew );
System.out.println( "dateTimeOld: " + dateTimeOld );
System.out.println( "period: " + period );
When run…
dateTimeNew: 2014-01-02T23:19:45.021Z
dateTimeOld: 2014-01-02T21:19:45.021Z
period: PT2H