views:

761

answers:

7

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?

+3  A: 

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

Daniel A. White
+1  A: 

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

Paul McKenzie
+6  A: 

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, which is a far superior date/time API. 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(...)

Jon Skeet
+1 thanks for the Joda link, that looks very nice.
GregS
+4  A: 

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.

missingfaktor
A: 

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"

jalf
There's more to avoiding it than just "then your code won't break if it's removed" - it's usually that the alternative is a better way of doing it in the first place (e.g. faster, more robust, better i18n support etc).
Jon Skeet
true, it's generally deprecated because a better alternative exists.
jalf
A: 

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

EJB
+7  A: 

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.

JuanZe
In Java 5 and newer, there is also a @Deprecated annotation (actually `java.lang.Deprecated`): http://java.sun.com/javase/6/docs/api/java/lang/Deprecated.html
R. Bemrose