stackUnderflow stackUnderflow - 1 year ago 151
Java Question

Parsing ISO 8601 duration format to Joda duration - IllegalArgumentException

I am trying to parse ISO 8601 duration of 1 month to Joda Duration object.
Can you please help me why this line of code

Duration duration = Duration.parse("P1M");


java.lang.IllegalArgumentException: Invalid format: "P1M"
at org.joda.time.convert.StringConverter.getDurationMillis(
at org.joda.time.base.BaseDuration.<init>(
at org.joda.time.Duration.<init>(
at org.joda.time.Duration.parse(

Answer Source

In this case, it should be a Period not a Duration as it is one month long so try this instead:

Period period = Period.parse("P1M");

Indeed a Duration needs to be expressed in an exact amount of milliseconds and as a month cannot be exactly expressed in milliseconds as it changes from one month to another it cannot be a Duration.

More details about Period and Duration here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download