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

我的问题是,当我进行解析时,输出是:

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

为什么?我无法理解原因!最后的日期是错误的,但只有最后的日期。例如,如果我更改日期,则输出再次错误。

4

3 回答 3

0

我相信您被自己的输入误导了:

strd.add("Sun May 11 03:17:40 UTC 2009");
strd.add("Mon May 11 03:17:40 UTC 2009");

2009 年 5 月 11 日是星期一。据我所知(但找不到任何来源),一周中的一天被一个月中的一天所覆盖。因此,您在解析为星期一后更改了星期日。

然后一切看起来都是正确的。在您的循环中,日期相等的唯一地方是:

k=0; h=2;
Date is ==>Mon May 11 03:17:40 UTC 2009

在循环中:

k=3; h=4; 
Date is ==>Thu Jun 11 03:17:40 UTC 2009

我不知道你是否故意这样做,但一个日期是2008 年

Date is ==>Wed Jun 11 03:17:40 UTC 2008
于 2012-11-16T00:22:52.637 回答
0

由于日期解析器没有使用星期几,因此打印输出中的结果对我来说是正确的。如果两个字符串之间的唯一区别是星期几,那么它们将解析为相同的日期值。

第一次通过循环:

"Sun May 11 03:17:40 UTC 2009" vs "Wed Jun 11 03:17:40 UTC 2008" == not equal month
"Sun May 11 03:17:40 UTC 2009" vs "Mon May 11 03:17:40 UTC 2009" == equal (DoW ignored)
"Sun May 11 03:17:40 UTC 2009" vs "Mon Jun 11 03:17:40 UTC 2009" == not equal month
"Sun May 11 03:17:40 UTC 2009" vs "Sun Jun 11 03:17:40 UTC 2009" == not equal month

第二次通过循环:

"Wed Jun 11 03:17:40 UTC 2008" vs "Mon May 11 03:17:40 UTC 2009" == not equal month
"Wed Jun 11 03:17:40 UTC 2008" vs "Mon Jun 11 03:17:40 UTC 2009" == not equal year
"Wed Jun 11 03:17:40 UTC 2008" vs "Sun Jun 11 03:17:40 UTC 2009" == not equal year

第三次通过循环:

"Mon May 11 03:17:40 UTC 2009" vs "Mon Jun 11 03:17:40 UTC 2009" == not equal month
"Mon May 11 03:17:40 UTC 2009" vs "Sun Jun 11 03:17:40 UTC 2009" == not equal month

第四次也是最后一次通过循环:

"Mon Jun 11 03:17:40 UTC 2009" vs "Sun Jun 11 03:17:40 UTC 2009" == equal (DoW ignored)
于 2012-11-16T00:34:09.693 回答
0

关于错误的日期,这是因为您使用的日期名称不是有效日期。以最后一个为例Sun Jun 11 03:17:40 UTC 2009。查一下日历,就是Thu那个日期。解析器知道这一点并输出正确的日期。

现在循环,如果你一步一步地跟随它,它没有错误,你会看到输出是正确的,因为它会检查List date自身。

最后,如果您使用的是 Java 6 或更早版本,则代码中的强制转换存在一些问题:

List<Date> date = new ArrayList<>();

不是真正有效的,而是应该是

List<Date> date = new ArrayList<Date>();

(感谢@Reimeus 指出这在 Java 7 中有效)

于 2012-11-16T00:38:16.797 回答