Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A non-deprecated exact equivalent of Date(String s) in Java?

Tags:

java

date

I have old code that uses new Date(dateString) to parse a date string. Compiling the code produces the deprecation warning Date(java.lang.String) in java.util.Date has been deprecated.

The javadoc unhelpfully advises me to use DateFormat.parse(), even though the DateFormat class does not have a static parse method.

Now, I know how to use SimpleDateFormat, but I want to make sure I'm getting the exact same behaviour of the deperecated Date constructor.

like image 936
itsadok Avatar asked Mar 18 '09 12:03

itsadok


People also ask

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 should I use instead of date in Java?

"The Calendar class should be used instead" - For Java 8 and later, the java. time. * classes are the better option ... if you are changing your code.

Is SimpleDateFormat deprecated?

Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.


1 Answers

Here's my guess (I posted as community wiki so you can vote up if I'm right):

Date parsed; try {     SimpleDateFormat format =         new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");     parsed = format.parse(dateString); } catch(ParseException pe) {     throw new IllegalArgumentException(pe); } 
like image 180
2 revs, 2 users 92% Avatar answered Sep 20 '22 07:09

2 revs, 2 users 92%