Just like the title says, I\'ve been able to isolate from a text file a string containing a duration in the format of \"HH:mm:ss.\" How would I go about converting it to a d
Duration.between (
LocalTime.MIN ,
LocalTime.parse ( "08:30:00" )
).toString()
PT8H30M
If by the string 08:30:00
you mean "eight and a half hours" span of time, then do not use the term "timestamp". The word “duration” is correct and commonly used. And avoid that format of HH:MM:SS because it is so ambiguous, appearing to be a time-of-day. Instead use the standard format discussed below.
If by the string 08:30:00
you mean “half-past eight in the morning”, then use the word 'timestamp' and avoid the term 'duration'.
These are two very different concepts. You must get clear on them, each should be distinct in your mind. Using the ambiguous format of HH:MM:SS makes that distinction all the more difficult (so avoid that format!).
The modern way is with the java.time classes.
LocalTime
As mentioned in the comment by Sharma, first parse your string as a LocalTime. This class represents a time-of-day without a date and without a time zone. Having no time zone means these objects are based on a generic 24-hour clock without regard for anomalies such as Daylight Saving Time (DST).
We do not really want a LocalTime
as your input string represents a span of time rather than a time-of-day. But this is just the first step.
LocalTime lt = LocalTime.parse ( "08:30:00" );
Duration
To represent the desired span-of-time, we want the Duration class. This class is for spans of time not attached to the timeline. We can create one by converting that LocalTime
via getting the amount of time from the beginning of the time-of-day clock, 00:00:00.0
or LocalTime.MIN, and the lt
we just instantiated.
Duration d = Duration.between ( LocalTime.MIN , lt );
We can see the result by generating a String in standard ISO 8601 format for durations by simply calling Duration::toString. The java.time classes use ISO 8601 by default when parsing/generating strings. For durations, the standard format is PnYnMnDTnHnMnS
where the P
marks the beginning and the T
separates the years-months-days portion from the hours-minutes-seconds portion. So, our eight-and-a-half hours will appear as PT8H30M
.
System.out.println ( "d.toString(): " + d );
d.toString(): PT8H30M
For handling such duration values, I strongly suggest using the Duration
object in your Java code and the ISO 8601 format when serializing to text.
08:30:00
which can be misread as a time-of-day.Your desired format of a decimal number like 8.5
is ambiguous (easy to lose track of its meaning). Also a decimal number is awkward and often incorrect when using a floating-point type (float
, Float
, double
, Double
) as these types generate extraneous incorrect digits at the far end of the decimal fraction. Floating-point technology purposely trades away accuracy to gain speed of execution.
BigDecimal
If you must use a fractional number, and want accuracy, use the BigDecimal class.
In this example, I assume you want hours and minutes while truncating any fractional minute. So I call toMinutes
on the Duration
.
BigDecimal minutesPerHour = new BigDecimal ( 60L ); // Use var/constant for clarity of your intent.
BigDecimal minutes = new BigDecimal ( d.toMinutes () );
BigDecimal fractionalHours = minutes.divide ( minutesPerHour );
frationalHours.toString(): 8.5
See this example code live in IdeOne.com.
You might want to use BigDecimal
facility for rounding if you insist on using these decimals rather than Duration
objects and ISO 8601 text.
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?
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.