Need to get the difference between two LocalDateTime objects?
If so, you're in luck.
Cuz what I'm gonna show you here will work with those objects as well.
So buckle in and enjoy the ride.
Our Friend ChronoUnit
We've chatted about ChronoUnit in the past. It's time to use it again.
Specifically, we're going to use the between() method.
Go to now:
LocalDateTime localDateTime = LocalDateTime.now(); System.err.println(localDateTime); LocalDateTime otherDateTime = localDateTime.minus(Period.ofDays(31)); System.err.println(otherDateTime); long difference = ChronoUnit.DAYS.between(otherDateTime, localDateTime); System.err.println(difference);
That's going to print out three (3) lines. The first two lines will vary depending on the date and time you run the code.
But that third line should stay constant. It should print out 31.
If you look at the first two blocks of code, you'll see that they instantiate two (2) LocalDateTime objects. The second one is 31 days prior to the first one.
That last block of code calculates the difference, in days, between the two objects.
Well, it's obviously 31 because the second block of code specifically instantiates a LocalDateTime object that's 31 days earlier than the first one.
So if you want to calculate the difference in time between two objects, use ChronoUnit to specify the time unit and invoke the between() method by passing in the two objects. It's that simple.
But It's Not That Simple
Keep in mind: that between() method is going to give you a signed difference.
Take a look at the order of the parameters: otherDateTime is first and localDateTime is second.
If you switch those two around, you'll get -31 instead of 31.
So if all you care about is the raw difference, just grab the absolute value of the number and don't worry about the order of the parameters.
However, if you want to know if one date is before or after another, you'll need to pay attention to the order of the parameters and the sign.
It works like this: the between() method calculates the difference by subtracting the first date from the second date.
That's why the difference is positive in the example above. But it's negative is you switch the order of the parameters.
You have another option when calculating the difference between two date/time objects: until().
But you'll still need to use good ol' ChronoUnit.
Take a look at this:
LocalDateTime localDateTime = LocalDateTime.now(); System.err.println(localDateTime); LocalDateTime otherDateTime = localDateTime.minus(Period.ofDays(31)); System.err.println(otherDateTime); long difference = otherDateTime.until(localDateTime, ChronoUnit.DAYS); System.err.println(difference);
Almost the same thing as the last block of code, but this time you're invoking a method directly on the LocalDateTime object instead of a third-party enum like ChronoUnit.
But you're still using ChronoUnit because you need to specify the unit of time.
Also: the until() method makes it clear which object is getting subtracted from the other object. So that's a cleaner way to do things if you're unsure about which object to put first in the between() method you saw above.
Wrapping It Up
That's all there is to it.
Feel free to take the code you've seen here and adapt it to suit your own requirements.
Photo by from