I often use Date.parse(String) to parse a date in an unknown format. It seems to work fine for my use cases so far, but I'm wondering why it was deprecated, and whether I should avoid it? The docs just say:
As of JDK version 1.1, replaced by DateFormat.parse(String s)
However, there's no static parse method in DateFormat, just an instance method, which requires a DateFormat object, which I won't have for an unknown date format.
So, I'm wondering...
Date.parse(String) was deprecated because it doesn't internationalize well. SimpleDateFormat or Calendar objects are preferred.
For personal uses where you know it parses dates well, you probably don't need to avoid it, but you should probably get in the habit of using the more standard options if you're going to be writing more serious date-sensitive code.
The simplest way now to parse a Date
is to use SimpleDateFormat, but you need to create an instance first there is no static method anymore.
SimpleDateFormat format = new SimpleDateFormat(pattern);
Date date = format.parse(strDate)
Date
and Calendar
are simply badly written because they are confusing and error prone, they are meant to totally be replaced by the version of joda time
that has been included into Java 8, so in anyway you should rely on this method anymore as it will simply be removed very soon.
So if you use Java 8, I highly encourage you to use directly DateTimeFormat
, if you have a previous version, you should use SimpleDateFormat
as mentioned above.
What you need to do is:
parse(String)
doesn't raise a ParseException
, something like this
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