I'd like to print the difference between two dates in a form like "2 days, 3 hours, 5 minutes and 8 seconds".
I'm using this code calling the Joda-Time library but I've some troubles.
Calendar cal = Calendar.getInstance();
DateTime firstSeen = new DateTime(cal.getTime());
cal.add(Calendar.HOUR, 24*8);
cal.add(Calendar.MINUTE, 10);
cal.add(Calendar.SECOND, 45);
DateTime lastSeen = new DateTime(cal.getTime());
Period period = new Period(firstSeen, lastSeen);
PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder()
.appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
String periodFormatted;
periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
What should be printed is "8 days, 10 minutes and 45 seconds". Instead I get "1 day, 10 minutes and 45 seconds"; It seems the days overflow into the week. How can I tell the formatter to ignore the overflows and sum what overflows into the first unit?