How to convert Days in Year(s), Month(s), and Day(s) with Jodatime?

How to convert Days in Year(s), Month(s), and Day(s) with Jodatime?

For example:

days = 365; should print = 1 Year(s), 0 Month(s), and 0 Day(s)

Days days = Days.days(365);
    Period p1 = new Period(days);

    PeriodFormatter dhm = new PeriodFormatterBuilder()
        .appendDays().appendSuffix(" days", " Dias").appendSeparator(", ")
        .appendMonths().appendSuffix(" months", " Meses").appendSeparator(", e ")
        .appendYears().appendSuffix(" years", " Anos").toFormatter();

    System.out.println(dhm.print(p1.normalizedStandard()));

Output console:

1 day(s)

I also tried print with

period.getYears() + " Ano(s), "
+ period.getMonths() + " Mes(es), "
+ period.getDays() + " Dia(s)"
Jon Skeet
people
quotationmark

There's basically no such concept. After all, 365 days isn't 1 year if you start in a leap year.

What you could do is take some "base" date, add your days-based period to that, and then take a year/month/day based period from the result:

Period convertToYearMonthDay(Period period, LocalDate referenceDate) {
    LocalDate endDate = referenceDate.plus(period);
    return new Period(referenceDate, endDate, PeriodType.yearMonthDay());
}

The reason for your current output is that normalizedStandard is normalizing to "52 weeks, 1 day".

people

See more on this question at Stackoverflow