Deprecated Date methods in Java?

TheJediCowboy picture TheJediCowboy · Jul 29, 2010 · Viewed 7k times · Source

What is really meant when using Java Date utilities and something has been deprecated. Does this mean that it is discouraged to use, or does it imply that it is forbidden?

I am guessing it is bad practice to use deprecated methods, but am not sure and wanted to find out.

For example, I am trying to use code such as the following

String date = request.getParameter("date"); 
model.setDate(new Date(date));

Of course...this is a high level example, but in this situation, my model uses type Date and I need to pull the date off the request as a String and create a date with it.

It works fine how I have it, but it is using a deprecated method.

EDIT - I have gone back and used

SimpleDateFormat formatter = new SimpleDateFormat(); 
model.setDate(formatter.parse(request.getParameter("date");



The date is in the format MM/DD/YYY like 07/23/2010 but I am getting a ParseException

What could this be from?

Answer

Andrzej Doyle picture Andrzej Doyle · Jul 29, 2010

You're right that this is bad practice. In almost all cases, deprecated methods tell you what to use instead, and this is no exception (see the Javadocs).

You're trying to create a Date out of a String. But what format is the String in? How should it be parsed? Is it UK or US date format?

The "proper" way to do this is to create an instance of SimpleDateFormat, and call its parse() method passing in your text string. This is guaranteed to work in future, and will be more robust now.