You can use the compareTo
-Method of LocalTime
if you convert the Date
before.
Convert like this (found at https://www.baeldung.com/java-date-to-localdate-and-localdatetime):
public static LocalTime convertToLocalTimeViaInstant(Date dateToConvert) {
return dateToConvert.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalTime();
}
and compare like this:
time1.compareTo(time2);
If u want to use a method u can use the conversion like this:
public static int compareTimeOfDates(Date date1, Date date2) {
return convertToLocalTimeViaInstant(date1).compareTo(convertToLocalTimeViaInstant(date2));
}