Feature, not a bug.
You are inappropriately using a date-time class to represent a time-of-day value. Also, you are using troublesome old date-time classes, now legacy, supplanted by the java.time classes.
Use LocalTime
class for a time-of-day only value, without a date and without a time zone or offset.
LocalTime lt = LocalTime.of( "22:15" ) ;
You can do math using the Duration
class.
Duration d = Duration.of( startLocalTime , stopLocalTime );
The catch with using a time-of-day-only value is wrapping around the clock, crossing over midnight. Without the context of dates, how could you expect a proper result? Going from "22:15" to "01:15" did you mean to roll into the next day, the next week, or the next year?
The Duration
class takes the approach of not crossing midnight. The range of values for LocalTime
is a total of twenty-four hours, running from the minimum of 00:00
to the maximum of 23:59:59.999999999
. So the only way to go from a later time to an earlier time is to travel backwards in time to the earlier point, resulting in a negative number.
LocalTime start = LocalTime.parse ( "22:15" );
LocalTime stop = LocalTime.parse ( "01:15" );
Duration d = Duration.between ( start , stop );
Dump to console.
System.out.println ( "start/stop: " + start + "/" + stop );
System.out.println ( "d.toString(): " + d );
start/stop: 22:15/01:15
d.toString(): PT-21H
That output is standard ISO 8601 format for durations.
If you want to calculate spans of time between actual moments on the timeline, search for Stack Overflow for java.time
, Duration
, Period
, ZonedDateTime
, ZoneId
, OffsetDateTime
, Instant
, Interval
, and “elapsed”. This topic has been covered many times already.
As a quick example, let’s assign some arbitrary dates running from January 23rd to 24th of this year in the zone of America/Montreal
. Result is three hours of elapsed time. This result might vary by date and zone because of anomalies such as Daylight Saving Time (DST).
ZoneId z = ZoneId.of( "America/Montreal" ) ;
ZonedDateTime zdtStart = ZonedDateTime.of( 2017, 1, 23, 22, 15, 0, 0 , z );
ZonedDateTime zdtStop = ZonedDateTime.of( 2017, 1, 24, 01, 15, 0, 0 , z );
Duration d = Duration.between( zdtStart , zdtStop );
zdtStart.toString(): 2017-01-23T22:15-05:00[America/Montreal]
zdtStop.toString(): 2017-01-24T01:15-05:00[America/Montreal]
d.toString(): PT3H
See that code run live at IdeOne.com.
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as java.util.Date
, Calendar
, & SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
Where to obtain the java.time classes?
- Java SE 8 and SE 9 and later
- Built-in.
- Part of the standard Java API with a bundled implementation.
- Java 9 adds some minor features and fixes.
- Java SE 6 and SE 7
- Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport.
- Android
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as Interval
, YearWeek
, YearQuarter
, and more.