How to find difference between two Joda-Time DateTimes in minutes
If you want the difference in minutes between two Joda-Time DateTime objects, the Minutes.minutesBetween(startDateTime, endDateTime).getMinutes() function is your friend.
Distinguishing Duration and Periods
It's vital to understand Duration and Period conceptually. Duration indicates a specified length of time in milliseconds, while Period represents a field-based timespan such as years, months, days etc.
For precision-based calculations with Duration, you'd do this:
Now, if you have periods which span daylight saving changes or involves months and years, use Period:
Managing across days boundaries
When differences involve day boundaries, or when your program has a curfew, adjustments using minusDays() help:
Fine-tuning computations
For moments when precision is a must, subtract the getMillis() values and convert the difference to minutes:
Beware, Joda-Time doesn't consider leap seconds, but this should work unless you're timing a rocket launch.
Closet full of computational errors
Remember to initialize DateTime objects accurately:
Poor initialization can cause discrepancies that put Gandalf to shame (you shall not parse!).
Mind the timezone
Like clothes, timezones can drastically change how your DateTime objects fit. If your objects exists in different time zones or spans daylight saving transitions, use Joda-Time's solid timezone handling. A stitch in time saves nine, right?
Was this article helpful?