I consider MadProgrammer’s suggestion to use truncatedTo best:
OffsetDateTime date1 = OffsetDateTime.parse("2018-05-25T13:55:24.000+05:30");
OffsetDateTime date2 = OffsetDateTime.parse("2018-05-31T08:31:00.000+05:30");
if (date1.truncatedTo(ChronoUnit.MINUTES).isAfter(date2.truncatedTo(ChronoUnit.MINUTES))) {
System.out.println("Is after ignoring seconds and smaller");
} else {
System.out.println("Is on or before ignoring seconds and smaller");
}
Output in this case:
Is on or before ignoring seconds and smaller
truncatedTo(ChronoUnit.MINUTES) sets seconds and smaller units (down to nanoseconds) to 0. As the date-time is immutable, a modified copy is returned. From the docs of the method:
Truncation returns a copy of the original date-time with fields
smaller than the specified unit set to zero. For example, truncating
with the minutes unit will set the second-of-minute and nano-of-second
field to zero.
If you are using ZonedDateTime, no problem, it too has a truncatedTo method with the same effect, as have even more of the classes of java.time.
LocalDateTimeand usedateTime.withSeconds(0).withNano(0)to zero out those values. You could also usetrunctaedTo(ChronoUnit.MINUTES)if that were easier