Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deprecated Date methods in Java?

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?

like image 589
TheJediCowboy Avatar asked Jul 29 '10 16:07

TheJediCowboy


2 Answers

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.

like image 171
Andrzej Doyle Avatar answered Oct 02 '22 19:10

Andrzej Doyle


A lot of people have mentioned what Deprecated means, but I don't see any explanation of why these methods are deprecated:

Sun (before they were part of Oracle) deprecated a number of methods in Date to get people to use the Calendar/GregorianCalendar classes for date manipulation instead.

like image 21
Powerlord Avatar answered Oct 02 '22 19:10

Powerlord