tl;dr
Instant now = Instant.now();
String output = Duration.between( now ,
now.plus( 473 , ChronoUnit.MILLISECONDS ) )
.toString();
PT0.473S
Details
As others stated, 473 milliseconds is less than a second so it appears as zero whole seconds.
ISO 8601
Do not track elapsed time using time-of-day formatting. It is ambiguous and confusing. And suffers from this kind of fractional problem depending on your format details.
Instead, use the standard ISO 8601 format for a span of time: PnYnMnDTnHnMnS
where P
marks the beginning and T
separates the years-months-days from the hours-minutes-seconds.
You are using troublesome old date-time classes now supplanted by the java.time classes.
Instant
The Instant
class represents a moment on the timeline in UTC with a resolution of nanoseconds.
Instant instant = Instant.now();
Let's simulate input data by getting a moment a few minutes in the future.
Instant future = instant.plus( 473 , ChronoUnit.MILLISECONDS );
Duration
The Duration
class represents a span of time unattached from the timeline.
Duration duration = Duration.between( instant , future );
Generate a String in ISO 8601 format.
String output = duration.toString();
PT0.473S
Internally tracked as a number of whole seconds plus a fractional second as a number of nanoseconds. So think of it as:
Duration = ( Seconds + Nanos )
You can ask for the total number of whole seconds and the fractional second as nanoseconds.
long seconds = duration.getSeconds();
int nanos = duration.getNano();
About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old date-time classes such as java.util.Date
, .Calendar
, & java.text.SimpleDateFormat
.
The Joda-Time project, now in maintenance mode, advises migration to java.time.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations.
Much of the java.time functionality is back-ported to Java 6 & 7 in ThreeTen-Backport and further adapted to Android in ThreeTenABP (see How to use…).
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.