aviad aviad - 6 months ago 16
Java Question

How to compare Joda DateTime objects with acceptable offset (tolerance)?

I wonder is there any standard API in

JodaTime
to compare 2
DateTime
objects with specified tolerance?
I am looking for a one-liner preferably by using
Joda
standard API. Not for time-aritmethic expressions like in this post.

Ideally, it would be something like:

boolean areNearlyEqual = SomeJodaAPIClass.equal(dt1, dt2, maxTolerance);


Thanks!

Answer

Use this:

new Duration(dt1, dt2).isShorterThan(Duration.millis(maxTolerance))
Comments