Java Reference
In-Depth Information
^
1 warning
C:\javasrc>
The warning is simple: the
Date
constructor that takes three integer arguments has been de-
precated. How do you fix it? The answer is, as in most questions of usage, to refer to the
javadoc documentation for the class. The introduction to the
Date
page says, in part:
The class
Date
represents a specific instant in time, with millisecond precision.
Prior to JDK 1.1, the class Date had two additional functions. It allowed the interpretation of
dates as year, month, day, hour, minute, and second values. It also allowed the formatting and
parsing of date strings. Unfortunately, the API for these functions was not amenable to interna-
tionalization. As of JDK 1.1, the
Calendar
class should be used to convert between dates and
time fields and the
DateFormat
class should be used to format and parse date strings. The cor-
responding methods in
Date
are deprecated.
And more specifically, in the description of the three-integer constructor, the
Date
javadoc
says:
Date(int year, int month, int date)
Deprecated. As of JDK version 1.1, replaced by
Calendar.set(year + 1900
,
month
,
date)
or
GregorianCalendar(year + 1900
,
month
,
date)
.
As a general rule, when something has been deprecated, you should not use it in any new
code and, when maintaining code, strive to eliminate the deprecation warnings.
main areas of deprecation warnings in the standard API are the really ancient “event hand-
ling” and some methods (a few of them important) in the
Thread
class.
You can also deprecate your own code, when you come up with a better way of doing things.
Put an
@Deprecated
annotation immediately before the class or method you wish to deprec-
tion is easier for some tools to recognize because it is present at runtime (so you can use Re-
flection (see
Chapter 23
)
.