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.
Date Class. Many of the methods in java. util. Date have been deprecated in favor of other APIs that better support internationalization.
"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.
Class SimpleDateFormat. Deprecated. A class for parsing and formatting dates with a given pattern, compatible with the Java 6 API.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With