简体   繁体   中英

Joda time - Not able to calculate months / years from Period in minutes

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;
}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM