This code will do the trick:
public static void findEvents(Map<Date, Event> dateEvents, Date targetDate) { SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy"); String target = dateFormat.format(targetDate); for (Map.Entry<Date, Event> entry : dateEvents.entrySet()) { if (dateFormat.format(entry.getKey()).equals(target)) { System.out.println("Event " + entry.getValue() + " is on the specified date"); } } }
It is important to note that all dates are converted to String with the format "dd.MM.yyyy" before comparison, so any differences in hours / minutes / seconds still match if the day is the same.
This code also demonstrates the best way (IMHO) to iterate over a map.
source share