使用 joda 查找从开始到结束的天数差异?

Finding difference of days from start and end using joda?

我想从 startDate 和 endDate 中找出现在是多少天。从我的 json 回复中,我可以在字符串中取回这些内容:

String startDate = "Fri, 10 Jan 2015 23:10:04 +0000";
String endDate = "Mon, 26 Jan 2015 11:26:04 +0000";

我如何使用 joda 计算并返回正确的天数(因此:numOfDays = endDate - startDate)??

public int getNumberOfDays(String startDate, String endDate) {
    String pattern = "EEE, dd MMM yyyy HH:mm:ss +xxxx";
    .....

    // quick test on startDate
    DateTime dateTime = DateTime.parse(startDate, DateTimeFormat.forPattern(pattern));
    System.out.println("The day should be 10 but it yields 9 --> " + dateTime.getDayOfMonth());

}

使用 Joda 库Class

Days days=Days.daysBetween(startDate.toLocalDate(), endDate.toLocalDate()).getDays();
int noOfDays=days.getDays();

作为替代方案,我们还可以使用 Java 8 日期和时间 API

public void days_between_two_dates_in_java_with_java8 () {

    LocalDate startDate = LocalDate.now().minusDays(1);
    LocalDate endDate = LocalDate.now();

    long days = Period.between(startDate, endDate).getDays();


    // or 

    long days2 = ChronoUnit.DAYS.between(startDate, endDate);

}