tl;dr
Duration.between( // Represent a span of time, unattached to the timeline.
earlier , // `Instant` class represents a moment in UTC with a resolution as fine as nanoseconds. Current moment captured in microseconds, depending on capability of host computer hardware and operating system.
Instant.now()
) // Returns a `Duration` object. Call `to…Part` methods to get the days, hours, minutes, seconds as separate numbers.
java.time
So much easier when using the modern java.time classes that supplanted the troublesome old date-time classes bundled with the earliest versions of Java.
Get the current moment in UTC. The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds (up to nine (9) digits of a decimal fraction).
The current moment is captured in milliseconds in Java 8 specifically. In Java 9 and later, you may see the current moment captured with a finer granularity due to a fresh implementation of Clock
. In Oracle Java 9 JDK on macOS Sierra, I get the current moment in microseconds (six digits of decimal fraction second).
Instant earlier = Instant.now() ;
…
Instant later = Instant.now() ;
Calculate elapsed time as a span-of-time unattached to the timeline. For a scale of hours-minutes-seconds (and days as 24-hour chunks of time unrelated to calendar), use Duration
. For a scale of years-months-days, use Period
.
Duration d = Duration.between( earlier , later ) ;
Generate a String is standard ISO 8601 format, PnYnMnDTnHnMnS
, where the P
marks the beginning and the T
separates the years-months-days from the hours-minutes-seconds.
String output = d.toString() ; // Standard ISO 8601 format.
I strongly recommend using the standard formats rather than the format seen in the Question. I have seen the time-of-day format cause so much confusion and ambiguity where it can be mistaken for, well, a time-of-day rather than a span-of-time. The ISO 8601 formats were wisely designed to avoid such ambiguities. But if you insist, in Java 9 and later you may ask for the parts of a Duration
by calling the to…Part
methods.
long days = d.toDaysPart() ; // 24-hour chunks of time, unrelated to calendar dates.
int hours = d.toHoursPart() ;
int minutes = d.toMinutesPart() ;
Then assemble as you desire into a string.
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.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for java.sql.*
classes.
Where to obtain the java.time classes?
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.