DateTimeZone timeZone = DateTimeZone.forID( "America/Montreal" );
DateTimeFormatter formatter = DateTimeFormat.forPattern( "yyyy/MM/dd" ).withZone( timeZone );
DateTime dateTimeStart = formatter.parseDateTime("2012/01/01");
DateTime dateTimeStop = formatter.parseDateTime("2017/06/12");
Period period = new Period( dateTimeStart, dateTimeStop );
PeriodFormatter periodFormatter = PeriodFormat.getDefault();
String output = periodFormatter.print( period);
System.out.println(output);
实际输出为:5 years, 5 months, 1 week and 4 days
我想要输出(推荐):5 years, 5 months, 11 days
答案 0 :(得分:1)
如果您阅读手册......
Period period = new Period(dateTimeStart, dateTimeStop, PeriodType.forFields(
new DurationFieldType[]{
DurationFieldType.years(),
DurationFieldType.months(),
DurationFieldType.days(),
DurationFieldType.hours(),
DurationFieldType.minutes(),
DurationFieldType.seconds(),
DurationFieldType.millis(),
}));