如何找到两个Joda-Time DateTime实例之间的天数差异? 我的意思是,如果开始是星期一,结束是星期二,我期望返回值为1,而不管开始日期和结束日期的小时/分钟/秒。

天。daysBetween(start, end). getdays()如果开始在晚上,结束在早上,则返回0。

我也有其他日期字段相同的问题,所以我希望有一种通用的方法来“忽略”较不重要的字段。

换句话说,2月到3月4日之间的月份也是1,14:45到15:12之间的时间也是1。但是14:01和14:55之间的小时差是0。


当前回答

令人恼火的是,withTimeAtStartOfDay答案是错误的,但只是偶尔错误。你想要的:

Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()

事实证明,“午夜/一天的开始”有时意味着凌晨1点(夏令时在某些地方是这样发生的)。daysBetween不能正确处理。

// 5am on the 20th to 1pm on the 21st, October 2013, Brazil
DateTimeZone BRAZIL = DateTimeZone.forID("America/Sao_Paulo");
DateTime start = new DateTime(2013, 10, 20, 5, 0, 0, BRAZIL);
DateTime end = new DateTime(2013, 10, 21, 13, 0, 0, BRAZIL);
System.out.println(daysBetween(start.withTimeAtStartOfDay(),
                               end.withTimeAtStartOfDay()).getDays());
// prints 0
System.out.println(daysBetween(start.toLocalDate(),
                               end.toLocalDate()).getDays());
// prints 1

通过LocalDate可以避免整个问题。

其他回答

你可以使用LocalDate:

Days.daysBetween(new LocalDate(start), new LocalDate(end)).getDays() 
DateTime  dt  = new DateTime(laterDate);        

DateTime newDate = dt.minus( new  DateTime ( previousDate ).getMillis());

System.out.println("No of days : " + newDate.getDayOfYear() - 1 );    

常量日期和当前日期之间的差异(Joda)

你可以使用Days.daysBetween(jodaDate1,jodaDate2)

这里有一个例子:

        val dateTime: DateTime = DateTime.parse("14/09/2020",
            DateTimeFormat.forPattern("dd/MM/yyyy"))
        val currentDate = DateTime.now()
        //To calculate the days in between
        val dayCount = Days.daysBetween(dateTime,currentDate).days
        //Set Value to TextView 
        binding.daysCount.text = dayCount.toString()
public static int getDifferenceIndays(long timestamp1, long timestamp2) {
    final int SECONDS = 60;
    final int MINUTES = 60;
    final int HOURS = 24;
    final int MILLIES = 1000;
    long temp;
    if (timestamp1 < timestamp2) {
        temp = timestamp1;
        timestamp1 = timestamp2;
        timestamp2 = temp;
    }
    Calendar startDate = Calendar.getInstance(TimeZone.getDefault());
    Calendar endDate = Calendar.getInstance(TimeZone.getDefault());
    endDate.setTimeInMillis(timestamp1);
    startDate.setTimeInMillis(timestamp2);
    if ((timestamp1 - timestamp2) < 1 * HOURS * MINUTES * SECONDS * MILLIES) {
        int day1 = endDate.get(Calendar.DAY_OF_MONTH);
        int day2 = startDate.get(Calendar.DAY_OF_MONTH);
        if (day1 == day2) {
            return 0;
        } else {
            return 1;
        }
    }
    int diffDays = 0;
    startDate.add(Calendar.DAY_OF_MONTH, diffDays);
    while (startDate.before(endDate)) {
        startDate.add(Calendar.DAY_OF_MONTH, 1);
        diffDays++;
    }
    return diffDays;
}

令人恼火的是,withTimeAtStartOfDay答案是错误的,但只是偶尔错误。你想要的:

Days.daysBetween(start.toLocalDate(), end.toLocalDate()).getDays()

事实证明,“午夜/一天的开始”有时意味着凌晨1点(夏令时在某些地方是这样发生的)。daysBetween不能正确处理。

// 5am on the 20th to 1pm on the 21st, October 2013, Brazil
DateTimeZone BRAZIL = DateTimeZone.forID("America/Sao_Paulo");
DateTime start = new DateTime(2013, 10, 20, 5, 0, 0, BRAZIL);
DateTime end = new DateTime(2013, 10, 21, 13, 0, 0, BRAZIL);
System.out.println(daysBetween(start.withTimeAtStartOfDay(),
                               end.withTimeAtStartOfDay()).getDays());
// prints 0
System.out.println(daysBetween(start.toLocalDate(),
                               end.toLocalDate()).getDays());
// prints 1

通过LocalDate可以避免整个问题。