0

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.

5
  • 1
    And what should be the meaning of the int primitive? What have you tried so far? Commented Nov 7, 2014 at 7: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. Commented Nov 7, 2014 at 7:38
  • Have you tried to find the spec for ISO 8601 and read it? Might help... Commented Nov 7, 2014 at 8:16
  • 2
    Your example string is not ISO 8601 format for a duration. Commented Dec 3, 2014 at 11:39
  • Similar Question: How to Parse Date from GMT TimeZone to IST TimeZone and Vice Versa in android Commented Dec 6, 2016 at 4:30

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

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" );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.