The constructor Date(…) is deprecated. What does i

2019-01-04 14:25发布

I'm trying to create a Date like this:

date = new Date(year-1900,mon-1,day,hrs,min,sec);

and Eclips gives me this warning: "The constructor Date(int,int,int,int,int) is deprecated".

What does it mean for a constructor to be deprecated? What can I do?

9条回答
聊天终结者
2楼-- · 2019-01-04 14:31

Deprecated means that it is a legacy or old way to do something and it should be avoided.

According to this document http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html, use Calendar.set(...).

查看更多
你好瞎i
3楼-- · 2019-01-04 14:32

Deprecated means the core API and other java libraries are not depends on it and it says you have better way(s).

查看更多
神经病院院长
4楼-- · 2019-01-04 14:35

Deprecated literally means disapproved of, but a more accurate translation would be retired. Deprecated means this method is still usable, but you should not use it. It will gradually be phased out. There is a new method to do the same thing. Deprecated methods are marked with a special Javadoc comment:

/**
 *@deprecated Please now use newMethod()
 *@see newMethod()
 */

Use:

  • Calendar.set(year + 1900, month, date, hrs, min)

or

  • GregorianCalendar(year + 1900, month, date, hrs, min).

As suggested by the API documentation.

查看更多
ら.Afraid
5楼-- · 2019-01-04 14:36

It means you shouldn't use it in new code. This is typically the case if there's now a better way of achieving something, but the old way is maintained for backward compatibility.

Instead, you could use the Calendar API, as the full message hopefully suggests to you - or (better IMO) you could use Joda Time or the java.time package in Java 8 (see the tutorial). Both of those are far superior date/time APIs. to the

When it comes to deprecated APIs, if the compiler message doesn't suggest an alternative, it's always worth looking at the Javadoc - which in this case suggests using Calendar.set(...).

查看更多
欢心
6楼-- · 2019-01-04 14:37

Here is a code snippet to help with migrating your code. Both prints are the same.

import java.util.Calendar;
import java.util.Date;

public class Tinker {

    public static void main(String[] args) {

        int Y = 2015; // Year 2015
        int M = 11;   // 0..11 -- December
        int D = 15;   // 15th
        int H = 16;   // 4:00 PM
        int MN = 28;  // 4:28 PM
        int S = 41;   // 4:28:41

        Date d = new Date(Y-1900,M,D,H,MN,S);

        System.out.println(d);

        Calendar c = Calendar.getInstance();
        c.set(Y, M, D, H, MN, S);

        d = c.getTime();
        System.out.println(d);                      

    }

}

The output:

Tue Dec 15 16:28:41 CST 2015
Tue Dec 15 16:28:41 CST 2015
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-04 14:40

Deprecated generally means that you're discouraged from using the function.

For some reason or another, it has been decided that Java would be better off without it (because a better alternative exists, and you should use that instead), and so it might be removed from a future version of Java. Deprecation is basically a warning that "this will probably get removed in the future, although we're keeping it around a bit longer to give you a chance to remove it from your code first"

查看更多
登录 后发表回答