繁体   English   中英

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

[英]actual argument Date cannot be converted to int by method invocation conversion

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没有显示任何错误,但是编译器发誓(

从您添加并说的内容来看,到现在为止,我将遵循我以前的直觉:在编译项目时(您是否只是在做“ javac”或其他事情?),您没有重新编译Task类,并且编译器正在使用旧版本。那个方法接受一个int而不是一个Date(就像我问的那样:您有这样的版本吗?)。

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

要解决此问题,请确保在编译其他代码之前重新编译Task类。

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

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

date 您应该传递一个int

您可以调用诸如start.getTime()/1000并将其转换为int

暂无
暂无

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

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