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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
snakile
  • 52,936
  • 62
  • 169
  • 241
  • If you're going to use dates don't forget to at least look at Apache Commons lang; all kinds of utilities (among which DateUtils) which makes dealing with dates (and strings) less of a hassle. See http://commons.apache.org/lang/ – extraneon Jan 04 '10 at 14:43
  • Still more important, at least since Java 8: look into [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 27 '19 at 16:31

10 Answers10

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.

Mirko Cianfarani
  • 2,023
  • 1
  • 23
  • 39
JuanZe
  • 8,007
  • 44
  • 58
  • 1
    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 – Powerlord Jan 04 '10 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 05 '11 at 18:09
  • If you're using `new GregorianCalendar(...)` you don't need to add 1900 to the year. – tterrace Mar 28 '13 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? – android developer Jan 21 '14 at 17:25
  • @androiddeveloper The answer to "Why?" Involves a long and twisted road from the first attempts at a thorough handling of date-time data with work dating back decades to Taligent, IBM, and later to Sun and the invention of Java. Long story short: Avoid these old classes (j.u.Date/.Calendar) and learn to use java.time (or Joda-Time) instead. – Basil Bourque Jul 25 '15 at 18:09
  • @BasilBourque Wait, there is a third try of them? I thought there was only Date and Calendar... What's the alternative to Calendar? – android developer Jul 26 '15 at 05:21
  • 1
    Yes, you should avoid j.u.Date/.Calendar. Read the [sibling answer by Jon Skeet](http://stackoverflow.com/a/1999784/642706) about Joda-Time and java.time. – Basil Bourque Jul 26 '15 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(...).

Addison
  • 7,322
  • 2
  • 39
  • 55
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 '12 at 05:43
  • @kamaci: As per my answer, you should use `Calendar` - or Joda Time. – Jon Skeet Sep 18 '12 at 05:59
  • Actually my question may be out off topic. I want to learn about the deprecated term. – kamaci Sep 18 '12 at 06: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 '12 at 06: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.". – Basil Bourque Jul 25 '15 at 18:24
  • The claim that the new way is strictly speaking a better way is kind of debatable - there's a more powerful way, but that isn't always better, more powerful classes like Calendar over Date do lead to bloat and performance drops, if literally all you want is YYYY MM DD, then calendar is overkill and the push to single, more powerful and bigger, class over simpler, more granular versions is why software is as huge in file size, and part of why it can be slow. – Andrew Feb 23 '20 at 17:53
  • @Andrew: Both `Date` and `Calendar` have been *disastrous* classes in the Java standard library, leading to confusion all over the place. I certainly wouldn't recommend (and didn't recommend) using `Calendar` - I'm recommending using the `java.time` API as a much, much simpler API to use correctly than `Date`. I can't imagine ever using `Date` and `Calendar` nowadays other than for compatibility and legacy reasons. When given the choice, `java.time` really *is* a better way. – Jon Skeet Feb 24 '20 at 07:26
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.

Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • 1
    Actually, both .Date and .Calendar have now been supplanted by the new [java.time package](http://docs.oracle.com/javase/8/docs/api/java/time/package-summary.html) built into Java 8 and later. – Basil Bourque Jul 25 '15 at 18:12
  • @BasilBourque, you are responding to an answer from over five years ago. – missingfaktor Jul 28 '15 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. – Basil Bourque Jul 28 '15 at 15:51
  • @BasilBourque, it might be better to update the answer itself with addendums. – missingfaktor Aug 03 '15 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. – Kevin Workman Aug 03 '15 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(...).

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
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
ChrisCantrell
  • 3,833
  • 1
  • 22
  • 14
2

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
  • 19,646
  • 25
  • 76
  • 120
1

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

EJB
  • 2,383
  • 14
  • 15
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

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
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"

jalf
  • 243,077
  • 51
  • 345
  • 550
  • 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 04 '10 at 14:22
  • true, it's generally deprecated because a better alternative exists. – jalf Jan 04 '10 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).

Premraj
  • 72,055
  • 26
  • 237
  • 180