How do I parse a string like "-8y5d" to a Period object in joda time
jodatime
Solution
Use the PeriodFormatterBuilder:
PeriodFormatter yearsAndMonths = new PeriodFormatterBuilder()
.printZeroRarelyFirst()
.appendYears()
.appendSuffix("y", "y")
.printZeroRarelyLast()
.appendDays()
.appendSuffix("d", "d")
.toFormatter();
System.out.println(yearsAndMonths.parsePeriod("8y5d").toDurationFrom(new DateTime()).getStandardDays());
Problem
I want to parse strings like "8y5d" or "-6y144d" into a joda time period object. Thanks