Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The parameterized constructors of the java.util.Date class are deprecated. What is the alternative?

What can I use to replace this, new Date(2009, 12, 9)?

Thanks for your help.

like image 694
Tim Avatar asked Dec 09 '09 14:12

Tim


People also ask

What can I use instead of Java Util date?

The standard alternate is using the Calendar Object. Calendar has one dangerous point (for the unwary) and that is the after / before methods. They take an Object but will only handle Calendar Objects correctly. Be sure to read the Javadoc for these methods closely before using them.

Is date class deprecated in Java?

Date Class. Many of the methods in java. util. Date have been deprecated in favor of other APIs that better support internationalization.

What is deprecated constructor?

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

Which action is used for date parameterization in Java?

The java. util. Date. after() method is used to check whether the current instance of the date is after the specified date.


1 Answers

Note: this answer was written in 2009. Since then, java.time has become the preferred date/time API in Java.


Ideally, use Joda Time instead. It's an infinitely superior API to the built-in one. You'd then want to choose between LocalDateTime and DateTime depending on your exact requirements (it's a complicated area - I'm not going to try to summarise in a sentence or two, but the docs do a good job).

If absolutely necessary, use a java.util.Calendar and convert that to a Date when you need to.

like image 70
Jon Skeet Avatar answered Sep 18 '22 14:09

Jon Skeet