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");
throws
java.lang.IllegalArgumentException: Invalid format: "P1M"
at org.joda.time.convert.StringConverter.getDurationMillis(StringConverter.java:111)
at org.joda.time.base.BaseDuration.<init>(BaseDuration.java:105)
at org.joda.time.Duration.<init>(Duration.java:209)
at org.joda.time.Duration.parse(Duration.java:59)
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
The Duration
is used to represent time-based amount of time, like seconds and nanoseconds.
To represent data-based amount of time, you should consider use Period
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With