Oddities when using joda to format certain time spans
I'm having hard time understanding what's going on here ... I'm trying to convert a time span to a string (i.e. "2 years, 6 days, 4 hours, 23 seconds").
A sample of the numbers I'm getting is the following:
Start: 1309605480723 End: 1341227880000
Start: Jul 2, 2011 4:18:00 AM End: Jul 2, 2012 4:18:00 AM span.toString: PT8783H59M59.277S formatter.print(span): 0 years, 0 months, 1 days, 23 hours, 59 minutes, 59 seconds span.toString(formatter): 0 years, 0 months, 1 days, 23 hours, 59 minutes, 59 seconds
The code that's producing the above is:
Period span = new Period(mEndTime - mStartTime);
PeriodFormatter formatter = new PeriodFormatterBuilder()
.printZeroAlways()
.appendYears().appendSuffix(" year", " years").appendSeparator(", ")
.appendMonths().appendSuffix(" month", " months").appendSeparator(", ")
.appendDays().appendSuffix(" day", " days").appendSeparator(", ")
.appendHours().appendSuffix(" hour", " hours").appendSeparator(", ")
.appendMinutes().appendSuffix(" minute", "开发者_如何学Go minutes").appendSeparator(", ")
.appendSeconds().appendSuffix(" second", " seconds")
.toFormatter();
Log.d(LOGTAG, "formatter.print: " + formatter.print(span.normalizedStandard()));
Log.d(LOGTAG, "span.toString: " + span.normalizedStandard().toString(formatter));
Can anyone explain these numbers?
Maybe I'm doing something stupid and can't see it (it's 4am afterall), but I just can't see why these 8783 hours can't even be up-converted to a few days? Thanks.Documentation to the rescue! :)
org.joda.time.Period.Period(long duration)
Creates a period from the given millisecond duration using the standard set of fields.
Only precise fields in the period type will be used. For the standard period type this is the time fields only. Thus the year, month, week and day fields will not be populated.
try:
Period span = new Period(mStartTime, mEndTime);
精彩评论