48

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, int) is deprecated".

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

2

10 Answers 10

46

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.

7
  • 1
    In Java 5 and newer, there is also a @Deprecated annotation (actually java.lang.Deprecated): java.sun.com/javase/6/docs/api/java/lang/Deprecated.html
    – Powerlord
    Jan 4, 2010 at 14:42
  • 15
    I fail to understand why new Date(2011,1,1) will be phased out in favor of creating a Calendar object and then setting it.
    – KalEl
    Jun 5, 2011 at 18:09
  • If you're using new GregorianCalendar(...) you don't need to add 1900 to the year.
    – tterrace
    Mar 28, 2013 at 19:24
  • 1
    what does the Calendar have that Date can't offer? Why couldn't they just add the new features to it instead? Jan 21, 2014 at 17:25
  • 1
    Yes, you should avoid j.u.Date/.Calendar. Read the sibling answer by Jon Skeet about Joda-Time and java.time. Jul 26, 2015 at 16:21
17

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

7
  • If there is not a better method to use but using existing method is dangerous can we use deprecated or should be there a new alternative?
    – kamaci
    Sep 18, 2012 at 5:43
  • @kamaci: As per my answer, you should use Calendar - or Joda Time.
    – Jon Skeet
    Sep 18, 2012 at 5:59
  • Actually my question may be out off topic. I want to learn about the deprecated term.
    – kamaci
    Sep 18, 2012 at 6:06
  • 1
    @kamaci: What about it though? Basically, it means "Please don't use this" - usually, either an alternative is provided, or it represents something you shouldn't be doing anyway.
    – Jon Skeet
    Sep 18, 2012 at 6:08
  • @kamaci To expand on Jon Skeet’s answer and comment, "deprecated" means the authors of that class are saying to you "We screwed up, this method or class was a bad idea. So please stop using this old code. Instead use our new nifty better-designed code. We want to remove this old code, and probably will do so eventually, but we want to give people time to transition.". Jul 25, 2015 at 18:24
5

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.

5
  • 1
    Actually, both .Date and .Calendar have now been supplanted by the new java.time package built into Java 8 and later. Jul 25, 2015 at 18:12
  • @BasilBourque, you are responding to an answer from over five years ago. Jul 28, 2015 at 15:32
  • Yes, I am. While posted five years ago, people are still reading it today. Readers deserve an updated mention about replacement for partially deprecated classes. Jul 28, 2015 at 15:51
  • @BasilBourque, it might be better to update the answer itself with addendums. Aug 3, 2015 at 16:35
  • @missingfaktor Whoops, didn't see the conversation between you two. I thought Basil's addition belonged as its own answer, but now I see you already asked him to add it to yours. I've reverted my reversion. Aug 3, 2015 at 20:30
4

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

4

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
2

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

1

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

1

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

0

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"

2
  • 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
    Jan 4, 2010 at 14:22
  • true, it's generally deprecated because a better alternative exists.
    – jalf
    Jan 4, 2010 at 14:26
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.