Duration.parse example

I am using jodatime's Duration class: http://joda-time.sourceforge.net/apidocs/org/joda/time/Duration.html to add a duration like 02:10 to a DateTime like 2014-08-02T11:34 using withDurationAdded(duration, 1).

I created duration using Duration.parse("02:10"). I get an IllegalFormatException for the "02:10". I don't see a formatSpecifier argument; how do I properly create this duration? The jodatime quickstart guide didnt provide an example of parse: http://joda-time.sourceforge.net/key_duration.html

Jon Skeet
people
quotationmark

The javadoc specifies the required format for Duration.parse:

Parses a Duration from the specified string.

This parses the format PTa.bS, as per AbstractDuration.toString().

You'll need to build a custom formatter - here's some sample code:

import org.joda.time.*;
import org.joda.time.format.*;

public class Test {
    public static void main(String[] args) {
        PeriodFormatter formatter = new PeriodFormatterBuilder()
            .appendHours()
            .appendLiteral(":")
            .appendMinutes()
            .toFormatter();
        Duration duration = formatter.parsePeriod("02:10").toStandardDuration();
        System.out.println(duration);
    }
}

people

See more on this question at Stackoverflow