java Date和LocalDateTime中一个月中的天数

Cha*_*lah 0 java datetime jodatime

对于startDate和endDate,如何确定以月和日为单位的时间段(剩下的内容):

String pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date start = format.parse("2016-11-09T02:00:00.000Z");
Date end = format.parse("2017-09-30T09:00:00.000Z");
Run Code Online (Sandbox Code Playgroud)

结果我想要:10个月和22天(结束日期)

Ant*_*iuc 5

如果可以使用Java 8,最好使用LocalDateTimePeriod:

ZonedDateTime dateTime = ZonedDateTime.parse("2016-11-09T02:00:00.000Z");
ZonedDateTime end = ZonedDateTime.parse("2017-09-30T09:00:00.000Z");
System.out.println(Period.between(dateTime.toLocalDate(),end.toLocalDate())); 
Run Code Online (Sandbox Code Playgroud)

结果是P10M21D10个月和21天