如何在joda时间内将像"-8y5d"这样的字符串解析为Period对象

use*_*628 9 jodatime

我想将像"8y5d"或"-6y144d"这样的字符串解析为joda时间段对象.谢谢

Ser*_*riu 11

使用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());
Run Code Online (Sandbox Code Playgroud)