简体   繁体   English

比较日期的方法似乎根本不起作用

[英]Method to compare dates doesn't seem to work — at all

public void display(Date date) {
        for (int i = 0; i < this.position; i++) {
            schedule[i].init();
            while (schedule[i].hasMoreOccurrences()) {
                Date tmp = schedule[i].nextOccurrence();
                if (tmp.compareTo(date) == 0) {
                    System.out.println(schedule[i].toString());
                    schedule[i].init();
                    break;
                }

            }
        }
    }

Basically put, the above goes through an array of "events". 基本上,上面经历了一系列“事件”。 Each event has a method to check is it has more occurrences and a method to get the next occurrence (which is of type date). 每个事件都有一个方法来检查它是否有更多的出现次数以及获取下一次出现的方法(类型为date)。

The method accepts a date and goes through the array and checks every event. 该方法接受日期并遍历数组并检查每个事件。 For each event it checks if it has more occurrences. 对于每个事件,它会检查是否有更多事件。 If it does, it checks if this occurrence is "equal" to the date specified. 如果是,则检查该事件是否与指定的日期“相等”。 If it is, it prints the event and breaks out of the loop and moves to the next event in the array. 如果是,则打印事件并跳出循环并移动到数组中的下一个事件。 If the next occurrence is not equal to the date given, it keeps checking as long as the event has more occurrences to check. 如果下一次出现不等于给定的日期,只要事件有更多要检查的事件,它就会继续检查。

I know this method is SUPPOSED to print out events, however I'm getting no errors and no output. 我知道这个方法是支持打印事件,但我没有错误,没有输出。 I've been playing around with it for a while to no avail. 我一直在玩它一段时间无济于事。

I'm 100% sure my hasMoreOccurrences() and nextOccurrence() methods are working correctly because other parts of the code which relies on them heavily works. 我100%确定我的hasMoreOccurrences()和nextOccurrence()方法正常工作,因为依赖于它们的代码的其他部分非常有用。

I'm stumped :/ Thanks for the help :) 我很难过:/感谢您的帮助:)

EDIT I've printed both tmp and date and I see that they both occur on the same DATE but not the same TIME. 编辑我已经打印了tmp和日期,我发现它们都出现在相同的日期但不是同一时间。 I don't care about the time, only about the date. 我不关心时间,只关心日期。 Any ideas? 有任何想法吗?

There are several ways to compare two Date instances without taking the time into account. 有几种方法可以比较两个Date实例而不考虑时间。

  1. Convert both to Calendar and reset the fields of desinterest. 将两者都转换为日历并重置desinterest字段。

     Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.set(Calendar.HOUR_OF_DAY, 0); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); 

    And then compare them. 然后比较它们。

     if (calendar1.equals(calendar2)) 

    (true, it's horrible, that's why JodaTime exist and JSR-310 is underway) (是的,这太可怕了,这就是为什么JodaTime存在且JSR-310正在进行中)

  2. Format to String without time and then parse back. 格式化为String而没有时间然后再解析。

     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); date = sdf.parse(sdf.format(date)); 

    And then compare them. 然后比较它们。

     if (date1.equals(date2)) 
  3. Use JodaTime from the beginning on. 从一开始就使用JodaTime

     if (dateTime1.toLocalDate().equals(dateTime2.toLocalDate())) 

Note that I used x.equals(y) instead of x.compareTo(y) == 0 because that's how you're supposed to compare objects on equality in Java. 请注意,我使用x.equals(y)而不是x.compareTo(y) == 0因为这就是你应该如何比较Java中相等的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM