1

Am using joda-time-2.5 in Android Studio project.

I am not able to work out what I missing to be able to correctly format a String with years and/or months.

The Period calculates correctly - but will not go beyond "Weeks" eg. 1000000 minutes is correctly formatted to "99wks, 1day, 10hrs + 40mins". But not as months/years format eg. "1year, 10months, 3weeks, 1day, 10hrs + 40mins" etc

I have tried all kinds of variations of

Period pA = new Period(mA);

Period pA = new Period(mA, PeriodType.standard());

Period pA = new Period(mA, PeriodType.yearMonthDay());

etc but these made no difference.

I have tried adding/removing various .appends/years/months/printZero - this made no difference.

I have tried changing the period units : if I use Months or Years it will work eg

    Months mA = Months.months(15);

    Period pA = new Period(mA, PeriodType.standard());

Correctly produces "1year, 3months".

I understand that 'years' and 'months' are not precise (and approximate would actually be fine in this case), but I thought that's what the PeriodTypes/yearMonthDay or standard took care of?

I also tried PeriodFormat.getDefault().print(period) without success.

Please find code below:

private String formatTimeStr(int minutes){

    Minutes mA = Minutes.minutes(minutes);

    Period pA = new Period(mA);

    PeriodFormatter dhm = new PeriodFormatterBuilder()
            .printZeroNever()
            .appendYears()
            .appendSuffix("year","years")
            .appendSeparator(", ")
            .appendMonths()
            .appendSuffix("mnth", "mnths")
            .appendSeparator(", ")
            .appendWeeks()
            .appendSuffix("wk", "wks")
            .appendSeparator(", ")
            .appendDays()
            .appendSuffix("day", "days")
            .appendSeparator(", ")
            .appendHours()
            .appendSuffix("hr", "hrs")
            .appendSeparator(" & ")
            .appendMinutes()
            .appendSuffix("min", "mins")
            .toFormatter();

    String formattedTimeStr = dhm.print(pA.normalizedStandard());

    return formattedTimeStr;
}
RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
  • You need a **reference timestamp** to add the minutes input on. Then you get two timestamps as base for calculation of `Period`, and then you should be able to convert the minutes to mixed months, days etc. – Meno Hochschild Jan 26 '15 at 11:18

1 Answers1

0

Fact is as you already have recognized that minutes are not convertible to months in a strict sense. The Joda-Time-documentation speaks it out, too.

If the period contains years or months, then the months will be normalized to be between 0 and 11. The days field and below will be normalized as necessary, however this will not overflow into the months field. Thus a period of 1 year 15 months will normalize to 2 years 3 months. But a period of 1 month 40 days will remain as 1 month 40 days.

Technically, there are two options for conversion.

a) You define a reference timestamp. Then you can do following:

LocalDateTime tsp1 = new LocalDateTime(); // now as example
LocalDateTime tsp2 = tsp1.plusMinutes(minutes);
Period p = new Period(tsp1, tsp2, PeriodType.standard()); // or other period type?

b) You find and develop yourself a rounding algorithm based on the estimated length of time units. For example you might be willing to accept a rounded month length of 30.5 days or similar. This can be considered as fair solution if the use-case does not require absolute precision as this is often true in social media scenarios. As far as I know, Joda-Time does not support such a rounding feature out of the box (in contrast to other libraries).

Meno Hochschild
  • 42,708
  • 7
  • 104
  • 126
  • Sigh. I did actually become desperate enough to actually read the documentation :) But somehow blew right past that paragraph. Thank you. I have opted for your first solution and it has worked as simply as 'copy and paste'. Cheers. – meLoveJodaTimeLongTime Jan 26 '15 at 20:01