Number of days between two dates in Joda-Time

后端 未结 8 1308
北恋
北恋 2020-11-22 09:07

How do I find the difference in Days between two Joda-Time DateTime instances? With ‘difference in days’ I mean if start is on Monday and end is on Tuesday I expect a return

8条回答
  •  无人共我
    2020-11-22 09:40

    The accepted answer builds two LocalDate objects, which are quite expensive if you are reading lot of data. I use this:

      public static int getDaysBetween(DateTime earlier, DateTime later)
      {
        return (int) TimeUnit.MILLISECONDS.toDays(later.getMillis()- earlier.getMillis());
      }
    

    By calling getMillis() you use already existing variables.
    MILLISECONDS.toDays() then, uses a simple arithmetic calculation, does not create any object.

提交回复
热议问题