19

我需要知道如何修复这些错误说明:

Note: Summer.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

这是我的代码:

import java.util.Calendar;
import java.util.*;

class Summer
{
    public static void main(String[] args)
    {
        Date d1 = new Date();
        Date j21 = new Date(d1.getYear(), 6, 21);
        if(d1.before(j21)) {
            long diff = j21.getTime() - d1.getTime();
            diff = diff / (1000 * 60 * 60 * 24);
            System.out.println("There are " + diff + " days until June 21st" );
        }
        else {
            long diff = d1.getTime() - j21.getTime();
            diff = diff / (1000 * 60 * 60 * 24);
            diff = 365 - diff;
            System.out.println("There are " + diff + " days until June 21st" );
        }
    }
}
4

5 回答 5

12

This is not an error; it's a warning message.

Your program would run as you wrote it.

The reason why the compiler is giving you this warning is because you have used a deprecated function call.

By "recompile with -Xlint", the compiler means to inform you that you need to recompile your program like this:

javac -Xlint abc.java 

If you do so, the compiler will tell you which methods are deprecated so you can remove your calls to them. (If some method is deprecated, it usually means that a better implementation is available and that you should use that instead of the deprecated method.)

于 2012-09-24T14:11:32.600 回答
8

这是一个警告。您正在使用已弃用的函数调用或对象。您可以像这样重新编译以找出它发生的位置:

javac -Xlint:deprecation Summer.java

通常,使用已弃用的库是个坏主意。它们可能会在下一个版本中消失。

于 2012-09-24T02:49:43.450 回答
2

正如消息所说,您需要使用-Xlint命令行切换到javac命令来编译它,如下所示:

C:\Temp>javac -Xlint Summer.java
Summer.java:22: warning: [deprecation] getYear() in java.util.Date has been deprecated
        Date j21 = new Date(d1.getYear(), 6, 21);
                          ^
Summer.java:22: warning: [deprecation] Date(int,int,int) in java.util.Date has been deprecated
        Date j21 = new Date(d1.getYear(), 6, 21);
               ^
2 warnings
于 2012-09-24T02:49:20.127 回答
-1
Note: filename.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

对于这个错误。但它实际上是缺少 try{}catch(){} 块的警告,您可以通过编写以下语句来查看受影响的代码

javac -Xlint:unchecked filename.java

它将显示未经检查的所有必须通过用户定义或系统定义的异常代码捕获的异常

于 2017-06-01T07:00:28.343 回答
-2

这些不是错误。只是警告。这些不会影响您的程序。但是,当您将来使用已弃用的 util 类 Date 时,它​​可能会影响您的程序。

最好使用它来java.util.Calendar 代替java.util.Date它提供与 Date 相同的功能和一些额外的功能

于 2012-09-24T10:47:48.453 回答