How to check if two ZonedDateTime objects represent the same day?

ZonedDateTime has been introduced in Java 8 and is a great choice when you would like to work with dates that contain time zone information.

It has an awesome truncatedTo(...) method, that lets you truncate it to specific parts. If we would like to check if two ZonedDateTime objects refer to the same day, we could use this method to first truncate them to days, then we can compare them using the equals method.

Here is an example of a method that does this:

private boolean isSameDay(ZonedDateTime date1, ZonedDateTime date2) {
    return date1.truncatedTo(ChronoUnit.DAYS).equals(date2.truncatedTo(ChronoUnit.DAYS));

Of course, if you want to do the check for something else (like hours, months, etc.), you can use a different ChronoUnit.