实际参数Date不能通过方法调用转换转换为int

时间:2013-10-18 17:54:29

标签: java compiler-errors

Javac对我说:

error: method nextTimeAfter in class Task cannot be applied to given types;
if (processed.nextTimeAfter(start) != null
^
required: int
found: Date
reason: actual argument Date cannot be converted to int by method invocation conversion

但在我的任务中,参数是Date,而不是int:

public Date nextTimeAfter(Date current) {
  // may return null or DATE
}

这是一个代码调用者:

public static Iterable<Task> incoming(Iterable<Task> tasks, Date start,
            Date end) {
        if (start.before(end))
            return tasks;
        LinkedList<Task> result = new LinkedList<Task>();
        for (Task processed : tasks) {
            if (processed.nextTimeAfter(start) != null
                    && processed.nextTimeAfter(start).before(end)) {
                result.add(processed);
            }
        }
        return result;
    }

另外,任务类

public class Task {
    private Date time;
    private Date startTime;
    private Date repeatInterval;
    private Date endTime;

    //...getters-setters

    public Date nextTimeAfter(Date current) {
        if (current == null)
               throw new IllegalArgumentException("Argument <current> is NULL");
        if (!isActive() || (!isRepeated() && current.after(getTime())))
            return null;
        Date result = getStartTime();
        while (result.after(current)) {
            Date temp = (Date) result.clone();
            temp.setTime(temp.getTime() + getRepeatInterval().getTime());
            if (temp.after(getEndTime()))
                return null;
            result.setTime(result.getTime() + getRepeatInterval().getTime());
        }
        return result;
    }
}

这很奇怪,为什么会如此。 Eclipse没有显示任何错误,但编译器发誓(

2 个答案:

答案 0 :(得分:1)

根据你添加的内容判断到现在为止我将继续我之前的预感:编译项目时(你只是在做“javac”还是其他什么?)你没有重新编译Task类,编译器正在使用一些旧版本,该方法接受int而不是Date(正如我所问:你有这样的版本吗?)。

必须在Eclipse中正确设置类路径和编译选项,因此它不会投诉。

要修复它,请确保在编译其他代码之前重新编译Task类。

答案 1 :(得分:0)

错误消息实际上是说nextTimeAfter需要int

required: int <-- what java expected
found: Date   <-- what you did

您通过了date。你应该传递int

您可以调用类似start.getTime()/1000的内容并转换为int