[英]Joda time - Not able to calculate months / years from Period in minutes
Am using joda-time-2.5 in Android Studio project. 我在Android Studio项目中使用joda-time-2.5。
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". 1000000分钟的格式正确设置为“ 99周,1天,10小时+ 40分钟”。 But not as months/years format eg. 但不是月/年格式,例如。 "1year, 10months, 3weeks, 1day, 10hrs + 40mins" etc “ 1年,10个月,3周,1天,10小时+ 40分钟”等
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. 我尝试添加/删除各种.appends/years/months/printZero
这没有区别。
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". 正确产生“ 1年3个月”。
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? 我知道“年”和“月”并不精确(在这种情况下,近似值实际上是可以的),但是我认为这是PeriodTypes / yearMonthDay或标准所负责的吗?
I also tried PeriodFormat.getDefault().print(period)
without success. 我也尝试了PeriodFormat.getDefault().print(period)
但没有成功。
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. Joda-Time文档也说明了这一点。
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. 如果期间包含年或月,则将月标准化为0到11之间。必要时将对天字段及以下天进行标准化,但是不会溢出到月字段中。 Thus a period of 1 year 15 months will normalize to 2 years 3 months. 因此,1年15个月的期限将正常化为2年3个月。 But a period of 1 month 40 days will remain as 1 month 40 days. 但是1个月40天的期限将保留为1个月40天。
Technically, there are two options for conversion. 从技术上讲,有两个转换选项。
a) You define a reference timestamp . a)您定义参考时间戳 。 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. b)您可以根据估计的时间单位长度找到并发展自己的舍入算法。 For example you might be willing to accept a rounded month length of 30.5 days or similar. 例如,您可能愿意接受30.5天或类似日期的舍入月份长度。 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). 据我所知,Joda-Time不开箱即用地支持这种舍入功能(与其他库相反)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.