Jodatime days type type problem / error

I have a problem with jodatime api. I can not understand why this test does not work. I need to decide how many days, hours, minutes and seconds I have in milliseconds. But the value of the days is not resolved .... any idea?

@Test public void testTime() { long secs = 3866 * 24 * 35; long msecs = secs * 1000; //Period period = duration.toPeriod(); PeriodType periodType = PeriodType.forFields( new DurationFieldType[]{ DurationFieldType.days(), DurationFieldType.hours(), DurationFieldType.minutes(), DurationFieldType.seconds(), }); Duration duration = Duration.standardSeconds(secs); Period period = duration.toPeriod(periodType, GregorianChronology.getInstance()); System.out.println("days:" + period.getDays()); System.out.println("hours:" + period.getHours()); System.out.println("mins:" + period.getMinutes()); System.out.println("seconds:" + period.getSeconds()); PeriodFormatter periodFormatter = new PeriodFormatterBuilder() .printZeroAlways() .minimumPrintedDigits(2) .appendDays() .appendSeparator(":") .appendHours() .appendSeparator(":") .appendMinutes() .appendSeparator(":") .appendSecondsWithOptionalMillis() .toFormatter(); StringBuffer stringBuffer = new StringBuffer(); periodFormatter.printTo(stringBuffer, period); System.out.println(">>>" + stringBuffer); } 

days off: 0 Hours: 902 min: 4 seconds: 0 00: 902: 04: 00

+4
source share
2 answers

You need to normalize the period using:

 Period normalizedPeriod = period.normalizeStandard(); 

or

 Period normalizedPeriod = period.normalizeStandardPeriodType(); 

Then you can use normalizedPeriod and see the results you are looking for. As a quick test, I modified your junit test case and added the line:

 period = period.normalizedStandard(); 

immediately after creating a period with a duration.

+1
source

The timeline you use cannot be considered accurate days. Instead of gregorianChronology, try using IOSChronology.getInstanceUTC() .

+1
source

All Articles