问题
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?
回答1:
This is the correct output, because the period is "1 week 1 day, 45 minutes" and week field is not printed:
package com.stackoverflow.so21002436;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
public class App {
public static void main(final String[] args)
{
final DateTime firstSeen = DateTime.now();
final DateTime lastSeen = firstSeen.plusDays(8).plusMinutes(10).plusSeconds(45);
final Period period = new Period(firstSeen, lastSeen);
System.err.println(period); // P1W1DT10M45S, ie. 1 day, 10 minutes and 45 seconds
final PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder().appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
final String periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
}
}
or use a specific PeriodType:
package com.stackoverflow.so21002436;
import org.joda.time.DateTime;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;
public class App {
public static void main(final String[] args)
{
final DateTime firstSeen = DateTime.now();
final DateTime lastSeen = firstSeen.plusDays(8).plusMinutes(10).plusSeconds(45);
final Period period = new Period(firstSeen, lastSeen, PeriodType.dayTime());
System.err.println(period); // P8DT10M45S, ie. 8 days, 10 minutes and 45 seconds
final PeriodFormatter daysHoursMinutesSeconds = new PeriodFormatterBuilder().appendDays()
.appendSuffix(" day", " days")
.appendSeparator(", ")
.appendMinutes()
.appendSuffix(" minute", " minutes")
.appendSeparator(" and ")
.appendSeconds()
.appendSuffix(" second", " seconds")
.toFormatter();
final String periodFormatted = daysHoursMinutesSeconds.print(period);
System.out.println(periodFormatted);
}
}
see Period(ReadableInstant, ReadableDuration, PeriodType)
来源:https://stackoverflow.com/questions/21002436/joda-time-period-print