问题
I want to parse strings like "8y5d" or "-6y144d" into a joda time period object. Thanks
回答1:
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());
来源:https://stackoverflow.com/questions/11669255/how-do-i-parse-a-string-like-8y5d-to-a-period-object-in-joda-time