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?
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(...)
.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:
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 thejava.time
package in Java 8 (see the tutorial). Both of those are far superior date/time APIs. to theWhen 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(...)
.Here is a code snippet to help with migrating your code. Both prints are the same.
The output:
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"