I have a specification which would return the payment history JSON after successful transaction. 3rd party JSON response has a field for the total time taken for the transaction. As example total time spent while doing the payment history was "00:10:10.0". How do I convert this format this String object to integer primitive.
-
1And what should be the meaning of the `int` primitive? What have you tried so far? – Norbert Radyk Nov 07 '14 at 07:28
-
@NorbertRadyk I want to convert this particular string to long/double/int form since the spec says it is a total time which the transaction took.I do a simple split and calculate the seconds .As it is mentioned in the spec as ISO 8601 format,hence the confusion.Wanted to know if there was anything specific to take care w.r.t the format. – Amar Nov 07 '14 at 07:38
-
Have you tried to find the spec for ISO 8601 and read it? Might help... – Uwe Allner Nov 07 '14 at 08:16
-
2Your example string is *not* [ISO 8601 format for a duration](http://en.m.wikipedia.org/wiki/ISO_8601#Durations). – Basil Bourque Dec 03 '14 at 11:39
-
Similar Question: [How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android](http://stackoverflow.com/q/14314426/642706) – Basil Bourque Dec 06 '16 at 04:30
2 Answers
If you don't mind using external library, then using Joda's org.joda.time.LocalTime
can help with the string parsing:
String duration = "00:10:10.0";
int seconds = LocalTime.parse(duration).getMillisOfDay() / 1000;
//returns 610
Please note, that since you're complying to ISO formatting you don't even need to explicitly specify the parsed format.
Also, if you're using Java 8 already, than Joda was used as an inspiration for the new date/time library available there, therefore you'll find a similar class in the standard library: LocalTime

- 2,608
- 20
- 24
The answer by Radyk is correct. Since the Question mentions ISO 8601 Duration, I will add that string output.
java.time
The java.time framework is built into Java 8 and later. Inspired by Joda-Time. Extended by the ThreeTen-Project. Brought to Java 6 & 7 by the ThreeTen-Backport project, and to Android by the ThreeTenABP project.
String durationAsLocalTimeString = "00:10:10.0";
LocalTime durationAsLocalTime = LocalTime.parse( durationAsLocalTimeString );
Duration duration = Duration.between( LocalTime.MIN , durationAsLocalTime );
String output = duration.toString();
PT10M10S
ISO 8601 Duration Format
That output of PT10M10S
is the standard Duration format of PnYnMnDTnHnMnS
defined by ISO 8601. The P
marks the beginning, the T
separates the years-months-days portion from the hours-minutes-seconds portion.
I suggest serializing to strings in this format. Using time-of-day format such as 00:10:10.0
to represent an elapsed time is confusing and error-prone. The ISO 8601 format is obvious and intuitive and solves the ambiguity problem.
Both java.time and Joda-Time use these ISO 8601 formats by default when parsing/generating textual representations of date-time values.
Duration duration = Duration.parse( "PT10M10S" );

- 303,325
- 100
- 852
- 1,154