I have some code that looks like "Sat May 12 04:46:05 EDT 2012" that is currently being parsed by the java.util.Date‘s Date(String s) constructor. However, now I am getting a warning in my IDE since it says that it is deprecated and the JavaDoc says:
Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
I tried using the SimpleDateFormatter but the default formatter is causing an exception, so I want to try using a pattern, but what is the pattern to parse like the String constructor does?
NB: this is different from other similar questions because it is specifically asking about replacing a deprecated (and popular) constructor, not just asking for help parsing an arbitrary date string.
The pattern is
EEE MMM d hh:mm:ss zzz YYYYso you can construct it usingnew SimpleDateFormat("EEE MMM d hh:mm:ss zzz YYYY").So code like
can be rewritten to:
edit: from the
java.util.Date.parse(String s)JavaDoc it turns out that the methodaccepts many syntaxes;so there will be no single matching pattern for all invocations you will encounter. May need to fiddle with the given pattern using the documentation fromjava.text.SimpleDateFormat.