Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java's Date(...) constructor is deprecated; what does that mean?

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?

like image 555
snakile Avatar asked Jan 04 '10 14:01

snakile


People also ask

What does deprecated date mean?

In several fields, especially computing, deprecation is the discouragement of use of some terminology, feature, design, or practice, typically because it has been superseded or is no longer considered efficient or safe, without completely removing it or prohibiting its use.

Why is date class deprecated?

SimpleDateFormat classes were rushed too quickly when Java first launched and evolved. The classes were not well designed or implemented. Improvements were attempted, thus the deprecations you've found. Unfortunately the attempts at improvement largely failed.

Is Java Util date deprecated?

util. Date has some serious design flows, from the day it was introduced. Many of its methods were deprecated since Java 1.1 and ported to (abstract) java. util.

What is wrong with Java Util date?

util. Date (just Date from now on) is a terrible type, which explains why so much of it was deprecated in Java 1.1 (but is still being used, unfortunately). Design flaws include: Its name is misleading: it doesn't represent a Date , it represents an instant in time.

What does deprecated methods mean in Java?

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:

What can I do with a deprecated constructor in Java?

As it is deprecated means that you ought not really use it. You could use Calendar to generate a date from fields instead. deprecated means the usage of this constructor is discouraged, and it may be removed in future releases of Java. Use the Calendar API. …and what can I do? Decide on a time zone. Use java.time, the modern Java date and time API.

Is the Date constructor deprecated in JDK 8?

See this article, Introducing the New Date and Time API for JDK 8. As the Date constructor is deprecated, you can try this code.

Why was the calendar class deprecated in Java?

It was deprecated because that sort of usage doesn't work well with internationalization. The Calendar class should be used instead: Calendar cal = Calendar.getInstance (); cal.set (Calendar.YEAR, 1988); cal.set (Calendar.MONTH, Calendar.JANUARY); cal.set (Calendar.DAY_OF_MONTH, 1); Date dateRepresentation = cal.getTime ();


8 Answers

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.

like image 120
JuanZe Avatar answered Oct 03 '22 21:10

JuanZe


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

like image 27
Jon Skeet Avatar answered Oct 03 '22 21:10

Jon Skeet


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.

like image 24
missingfaktor Avatar answered Oct 03 '22 21:10

missingfaktor


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

like image 27
Daniel A. White Avatar answered Oct 03 '22 22:10

Daniel A. White


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
like image 45
ChrisCantrell Avatar answered Oct 03 '22 21:10

ChrisCantrell


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

like image 32
Paul McKenzie Avatar answered Oct 03 '22 21:10

Paul McKenzie


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

like image 27
EJB Avatar answered Oct 03 '22 22:10

EJB


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

  • Oracle tutorial: Date Time explaining how to use java.time.
  • Enhanced Deprecation in Oracle JDK 9 Documentation.
like image 45
Ole V.V. Avatar answered Oct 03 '22 21:10

Ole V.V.