views:

53

answers:

3

How do i find the difference in Days between two DateTime instances? with difference in days i mean if start is on Monday and end is on Tuesday i expect a return value of 1 regardless the hour/minute/seconds of the start and end dates.

Days.DaysBetween(start, end).getDays() gives me 0 if start is in the evening and end in the morning.

Edit: i'm also having the same issue with other date fields so i was hoping there would be a generic way to 'ignore' the fields of lesser significance.

IE. the months between Feb and 4 march would also be 1, as would the hours between 14:45 and 15:12 be. however the hour difference between 14:01 and 14:55 would be 0

+1  A: 
Interval interval = new Interval(oldTime, new Instant());   

or

From here

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

Ref:

org.life.java
Why downvote ????
org.life.java
It wasn't me, but I would guess it's because you suggested something that the author said was not working.
Bozho
@Bozho , Thanks for clearing this.
org.life.java
+1  A: 

you can use DateMidnight:

Days.DaysBetween(new DateMidnight(start), new DateMidnight(end)).getDays() 
Bozho
+2  A: 

This should work:

Days.daysBetween(start.toDateMidnight() , end.toDateMidnight() ).getDays() 
Michael Borgwardt