0

I have this code:

public static int getDaysBetween(Date dateA, Date dateB) {
    return Days.daysBetween(
        new DateTime(dateA), 
        new DateTime(dateB))
    .getDays();
}

And this test case:

@Test
public void test() {
    Date dateFrom = buildDate(10, Calendar.JANUARY, 2018);
    Date dateTo = buildDate(1, Calendar.JANUARY, 2018);
    assertEquals(DateUtils.getDaysBetween(dateFrom, dateTo), -9);
}

Test passes most of the times, but it fails randomly. Sometimes, getDaysBetween returns -8 instead of -9.

What's wrong? Is it any JodaTime bug?

Héctor
  • 24,444
  • 35
  • 132
  • 243

1 Answers1

1

It isn't technically a bug, just an annoyance. Turns out of the difference between your 2 days doesn't reach a day, it returns 0. And if you've 1.99999 days, it returns 1.

Theres a post for a very similar issue that could help you here

Jorge.V
  • 1,329
  • 1
  • 13
  • 19