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

时间:2012-07-26 12:25:28

标签: jodatime

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

1 个答案:

答案 0 :(得分: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());