tl;dr
Duration.between(
LocalTime.of( 12 , 5 , 0 ) ,
LocalTime.of( 12 , 7 , 0 )
).toString()
PT2M
If, against my strong recommendation to use Duration
, you insist on using ambiguous/confusing time-of-day formatting:
LocalTime.MIN.plus(
Duration.between(
LocalTime.of( 12 , 5 , 0 ) ,
LocalTime.of( 12 , 7 , 0 )
)
).toString();
00:02
To force all three components (hours, minutes, seconds), use the predefined DateTimeFormatter.ISO_LOCAL_TIME
.
LocalTime.MIN.plus(
Duration.between(
LocalTime.of( 12 , 5 , 0 ) ,
LocalTime.of( 12 , 7 , 0 )
)
).format( DateTimeFormatter.ISO_LOCAL_TIME )
00:02:00
See live code in IdeOne.com.
Moment != span-of-time
Do not abuse a date-time class such as java.util.Date
to store elapsed time. Such a class represents a moment, not a span of time.
Avoid legacy date-time classes
Do not use troublesome old date-time classes such as java.util.Date
. Those are now supplanted by java.time classes.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Get the current moment.
Instant now = Instant.now();
Simulate a later time.
Instant future = now.plus( Duration.ofMinutes( 5 ) );
Duration
Capture elapsed time with a Duration
or Period
object. Each of these represent a span of time, the first handles days-hours-minutes-seconds and the second handles years-months-days.
Duration duration = Duration.between( now , future );
String format
To present that duration value as a String, do not use time-of-day formatting as that is ambiguous. Instead use standard ISO 8601 format for durations. This format, PnYnMnDTnHnMnS
, marks the beginning with a P
. The T
in the middle separates the years-months-days portion from the hours-minutes-seconds portion. For example, two and a half hours is PT2H30M
. Our example here of five minutes is PT5M
.
String output = duration.toString(); // PT5M
LocalTime
If you do have an actual time-of-day and want to format with padding zeros, use the default format of LocalTime::toString
.
LocalTime.now( ZoneId.of( "America/Montreal" ) ).toString(); // 02:03:04.789Z
Another example. Again, I do not recommend abusing LocalTime
this way. (Instead, stick with Duration
for elapsed time.)
LocalTime start = LocalTime.now( ZoneId.of( "America/Montreal" ) );
LocalTime stop = start.plusMinutes( 7 );
Duration d = Duration.between( start , stop );
LocalTime result = LocalTime.MIN.plus( d ); // I do *not* recommend abusing `LocalTime` this way. Use `Duration` instead for elapsed time.
DateTimeFormatter f = DateTimeFormatter.ISO_LOCAL_TIME ;
String output = result.format( f );
System.out.println( "output: " + output );
output: 00:07:00
See live code in 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.