Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Date object constructor for getting string is deprecated

I have problem using Date in java, I have a string that represent a date for example String date = "23/10/2012"; and i have a function that get a date as parameter, for example

public void foo(Date date){...}

my problem is that Date constructor that accept a string is deprecated and i do not want to use a deprecated object.

like image 980
Yo Al Avatar asked Dec 06 '25 04:12

Yo Al


1 Answers

So, you look at the Javadoc for the deprecated method which specifies:

Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).

You then look up DateFormat.parse and proceed accordingly. You probably want to create a SimpleDateFormat - or better still, use Joda Time which is a much better date/time API.

When a method is deprecated, it's always a good idea to give an indication of what existing code should be migrated to, and ideally why the old method was deprecated. Follow this guidance when you deprecate your own methods... and whenever you try to use a deprecated method, check the documentation to see if it suggests a better alternative.

like image 111
Jon Skeet Avatar answered Dec 07 '25 16:12

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!