Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I parse a string like "-8y5d" to a Period object in joda time

Tags:

jodatime

I want to parse strings like "8y5d" or "-6y144d" into a joda time period object. Thanks

like image 875
user1554628 Avatar asked Jul 26 '12 12:07

user1554628


1 Answers

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());
like image 188
Sergiu Dumitriu Avatar answered Oct 22 '22 18:10

Sergiu Dumitriu