4

I have a hashmap: Map dateEvent = new HashMap(); where key is a date and time and value is a string. I fill collection with data where date is in format dd.MM.yyyy HH:mm. How I can get all keys with date based on this format: dd.MM.yyyy?

user902201
  • 43
  • 1
  • 1
  • 3
  • I hope you are using generics and you have forgotten to write it here. Don't ever use raw types. – user219882 Aug 19 '11 at 10:11
  • 2
    When you say key is a date and time do you mean a `Date` object or a `String` object in the form `dd.MM.yyyy HH:mm`? – Qwerky Aug 19 '11 at 10:13
  • 1
    http://stackoverflow.com/questions/6261237/sort-keys-which-are-date-entries-in-a-hashmap this may help you. It's not the exact case as yours but there is a useful code with the Date as a key. – user219882 Aug 19 '11 at 10:15
  • What do you mean by "date based on this format: dd.MM.yyyy" ? All your keys are of same format right ? – Swagatika Aug 19 '11 at 10:16

4 Answers4

11

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");
        }
    }
}

The important thing here is that all dates are converted to a String with format "dd.MM.yyyy" before comparing, so any differences in hour/minute/second still match if the day is the same.

This code also demonstrates the best way (IMHO) to iterate over a map.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • If you need both key and value this sure is the best way to iterate over a map. Code analyzers such as FindBugs will note this too. – musiKk Aug 19 '11 at 10:43
0

Not sure whether I get you right. However, you get the set of keys from a map using map.keySet(). If you want to find all different dates fill all dates into a set. If you want to reduce the accuracy to days, one solution would be to convert the date to the desired format and add those strings to a set. Duplicates will be removed automatically.

E.g.:

Map<Date, String> yourMap = [..];

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd.MM.yyyy");

Set<String> differentDates = new HashSet<String>();
for (Date date: yourMap.keySet()) {
    differentDates.add(simpleDateFormat.format(date));
}
Chris
  • 8,031
  • 10
  • 41
  • 67
0

You have (at least) two options:

You could write your own Date class which supplies appropriate implementations of hashCode() and equals(). (If you do this, it is not recommended that you base that class on another class that already defines these methods (e.g. java.util.Date).)

The brute force alternative is to scan all keys whether they fit your criteria.

musiKk
  • 14,751
  • 4
  • 55
  • 82
  • "(If you do this, it is not recommended that you base that class on another class that already defines these methods (e.g. java.util.Date).)" why? – Dejell Oct 13 '14 at 17:46
  • @Dejel Pretty sure this goes against the contract. [`java.util.Date#equals()`](http://docs.oracle.com/javase/7/docs/api/java/util/Date.html#equals(java.lang.Object)) states the two dates have to be equal to the millisecond in order to be equal. – musiKk Oct 14 '14 at 06:37
0

There is no difference between between the Date 01.01.2011(dd.MM.yyyy) and the Date 01.01.2011 00:00(dd.MM.yyyy HH:mm).

Date holds a long which has hours and minutes. Even for public Date(int year, int month, int date)

oliholz
  • 7,447
  • 2
  • 43
  • 82