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 value of 1 regardless of 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.
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.
In other words, 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.
you can use
LocalDate
:tl;dr
…or…
java.time
FYI, the Joda-Time project is now in maintenance mode, with the team advising migration to the java.time classes.
The equivalent of Joda-Time
DateTime
isZonedDateTime
.Apparently you want to count the days by dates, meaning you want to ignore the time of day. For example, starting a minute before midnight and ending a minute after midnight should result in a single day. For this behavior, extract a
LocalDate
from yourZonedDateTime
. TheLocalDate
class represents a date-only value without time-of-day and without time zone.Use the
ChronoUnit
enum to calculate elapsed days or other units.Truncate
As for you asking about a more general way to do this counting where you are interested the delta of hours as hour-of-the-clock rather than complete hours as spans-of-time of sixty minutes, use the
truncatedTo
method.Here is your example of 14:45 to 15:12 on same day.
For a count of days by dates, truncate to
ChronoUnit.DAYS
. Here is an example rolling over midnight from five minutes before to five minutes after, for elapsed days of1
.About java.time
The java.time framework is built into Java 8 and later. These classes supplant the troublesome old legacy date-time classes such as
java.util.Date
,Calendar
, &SimpleDateFormat
.The Joda-Time project, now in maintenance mode, advises migration to the java.time classes.
To learn more, see the Oracle Tutorial. And search Stack Overflow for many examples and explanations. Specification is JSR 310.
You may exchange java.time objects directly with your database. Use a JDBC driver compliant with JDBC 4.2 or later. No need for strings, no need for
java.sql.*
classes.Where to obtain the java.time classes?
The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.The ThreeTen-Extra project extends java.time with additional classes. This project is a proving ground for possible future additions to java.time. You may find some useful classes here such as
Interval
,YearWeek
,YearQuarter
, and more.Annoyingly, the withTimeAtStartOfDay answer is wrong, but only occasionally. You want:
It turns out that "midnight/start of day" sometimes means 1am (daylight savings happen this way in some places), which Days.daysBetween doesn't handle properly.
Going via a
LocalDate
sidesteps the whole issue.Days
ClassUsing the
Days
class with thewithTimeAtStartOfDay
method should work:The accepted answer builds two
LocalDate
objects, which are quite expensive if you are reading lot of data. I use this:By calling
getMillis()
you use already existing variables.MILLISECONDS.toDays()
then, uses a simple arithmetic calculation, does not create any object.