在计算joda时间的日期差异时,应显示天数而不是星期

时间:2017-02-18 08:58:54

标签: java date jodatime period

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

1 个答案:

答案 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(),
        }));