public static void main(String[] args) throws ParseException {
List<String> strd = new ArrayList<>();
List<Date> date = new ArrayList<>();
strd.add("Sun May 11 03:17:40 UTC 2009");
strd.add("Wed Jun 11 03:17:40 UTC 2008");
strd.add("Mon May 11 03:17:40 UTC 2009");
strd.add("Mon Jun 11 03:17:40 UTC 2009");
strd.add("Sun Jun 11 03:17:40 UTC 2009");
List<Character> letters = new ArrayList<>();
char letter;
for (letter = 'A'; letter <= 'Z'; letter++) {
letters.add(letter);
}
List<String> dateToChar = new ArrayList<>();
for (int j = 0; j < strd.size(); j++) {
try {
String strDate = strd.get(j);
// String strDate = "Mon May 11 03:17:40 UTC 2009";
SimpleDateFormat formatter = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
Date dateStr = formatter.parse(strDate);
String formattedDate = formatter.format(dateStr);
System.out.println("Date is ==>" + formattedDate);
Date date1 = formatter.parse(formattedDate);
date.add(date1);
} catch (ParseException e) {
System.err.print(e);
}
}
for (int k = 0; k < date.size(); k++) {
for (int h = 1; h < date.size(); h++) {
if (k != h && k < h) {
if (date.get(k).equals(date.get(h))) {
System.out.println("Equal dates");
} else {
System.out.println("Not Equal dates");
}
}
}
}
}
}
My problem is that when I do the parsing, the output is:
Date is ==>Mon May 11 03:17:40 UTC 2009
Date is ==>Wed Jun 11 03:17:40 UTC 2008
Date is ==>Mon May 11 03:17:40 UTC 2009
Date is ==>Thu Jun 11 03:17:40 UTC 2009
Date is ==>Thu Jun 11 03:17:40 UTC 2009
Not Equal dates
Equal dates
Not Equal dates
Not Equal dates
Not Equal dates
Not Equal dates
Not Equal dates
Not Equal dates
Not Equal dates
Equal dates
Why? I cannot understand the reason! The last dates are wrong, but only the last ones. If I change the date, for example, the output is wrong again.
The results in your print out look correct to me as the Day of Week is not being used by the date parser. If the only difference between two of the strings is the day of the week, then they will parse to the same date value.
First pass thru loop:
Second pass thru loop:
Third pass thru loop:
Fourth and final pass thru loop: