开发者

Java's Date(...) constructor is deprecated; what does that mean?

I'm trying to create a Date like this:

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

and Eclipse gives me this warning: "The constructor Date(int, int, int, int, i开发者_JAVA技巧nt) is deprecated".

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


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.


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(...).


That means you shouldn't be using it in new code typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code.

In your case, you can use java.util.Calendar class instead of java.util.Date.

By the way, in Java 8 and later, these old classes are supplanted by the new java.time package (Tutorial). Inspired by Joda-Time, defined by JSR 310, and extended by the ThreeTen Extra project. The old classes remain in place and you may continue to use them (while avoiding their deprecated parts), but you are encouraged to transition to the new classes.


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(...).


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


As it is deprecated means that you ought not really use it. You could use Calendar to generate a date from fields instead.


deprecated means the usage of this constructor is discouraged, and it may be removed in future releases of Java. Use the Calendar API.


java.time

…and what can I do?

  1. Decide on a time zone.
  2. Use java.time, the modern Java date and time API.

For example:

    ZoneId zone = ZoneId.of("America/Glace_Bay");
    ZonedDateTime dateTime = ZonedDateTime.of(2019, 10, 27, 12, 34, 56, 0, zone);
    System.out.println(dateTime);

Output is:

2019-10-27T12:34:56-03:00[America/Glace_Bay]

Don’t use Date. That class was always poorly designed. There is a reason why most constructors and most methods were deprecated very quickly. They don’t work reliably across time zones. Today we have so much better in java.time.

Only if you need a Date for a legacy API that you cannot afford to upgrade to java.time just now, convert like this:

    Instant pointInTime = dateTime.toInstant();
    Date oldFashionedDate = Date.from(pointInTime);
    System.out.println(oldFashionedDate);

Output in my time zone is:

Sun Oct 27 15:34:56 GMT 2019

The time of day doesn’t agree with what we specified. This is because I am in a different time zone. Date.toString (called from System.out.println) renders the date in the default time zone of the JVM, which has confused many over the years (it’s just one of the many reasons to avoid that class). The Date does represent the correct point in time, and you can use it for your legacy API.

What does it mean for a constructor to be deprecated,…

Many have explained that nicely already. I haven’t taken the deprecation from the 1990s as a promise to remove the constructor. In Java 9 they introduced the possibility of marking a deprecated item for removal. I am still curious to see whether they will mark this constructor for removal at some point.

Links

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Enhanced Deprecation in Oracle JDK 9 Documentation.


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"


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜