Damn Vegetables
Damn Vegetables

Reputation: 12494

Joda time PeriodFormatterBuilder, can I omit weeks and use only days?

I have seen this, but is it possible to use only month and days, not weeks?

For example, instead of "1 month 2 weeks 2 days", I want "1 month 16 days".

Upvotes: 0

Views: 314

Answers (1)

user9306023
user9306023

Reputation: 28

You can use a solution similar to the answer you linked, but you'll also need to use a org.joda.time.PeriodType to normalized the period:

// 1 month, 2 weeks and 2 days
Period p = new Period(0, 1, 2, 2, 0, 0, 0, 0);

PeriodFormatter fmt = new PeriodFormatterBuilder()
    // months
    .appendMonths().appendSuffix(" month", " months").appendSeparator(" and ")
    // days
    .appendDays().appendSuffix(" day", " days")
    .toFormatter();
System.out.println(fmt.print(p.normalizedStandard(PeriodType.yearMonthDayTime())));

This will print:

1 month and 16 days

Upvotes: 1

Related Questions