So I am coming back to Java after a few years of .NET programming and I am trying to update my Java knowledge but am having some problems with the now deprecated Date.parse() function that I once knew. I am trying to indiscriminately parse a string to a date. I do NOT want to have to use the SimpleDateFormtter where I need to specify every acceptable format. I just want to do what Date.parse(String anyStringFormat) to get epoch or new Date(String anyStringFormat) to get new date - what these guys used to do. Every document I keep getting directed to says 开发者_JS百科I have to use the SimpleDateFormat with a specified date format. I do not wish to use a specific date format - what is the new way to do this without a specific date format? I must be missing something, as I can't imagine they would remove such a feature.
Every document I keep getting directed to says I have to use the SimpleDateFormat with a specified date format. I do not wish to use a specific date format - what is the new way to do this without a specific date format? I must be missing something, as I can't imagine they would remove such a feature.
You're doing it wrong then. new Date(String)
was deprecated for the exact reasons mentioned in the comments, because date strings can be ambiguous and therefore the returned parse value may not be what you (or your user) expects. This is why DateFormat.parse(String)
exists.
If you don't want to use SimpleDateFormat because you don't want to have to specify the exact date string format to use, you are free to implement your own subclass of DateFormat
and the parse(String)
method - but I expect you will have a lot of trouble implementing logic to deal with ambiguous date strings.
edit: If you are sending a String to your database where it will be formatted anyway, then why even bother parsing a String into a java.util.Date
in your app? Just send the raw String to the database and you never need to deal with date parsing issues, nor do you waste any CPU cycles on unnecessary parsing.
You need to use SimpleDateFormat
(or DateTimeFormat
from joda-time).
There are too many options for date formats and you can't count on a method to try all of them. If you want to support more than one format, make a list of supported patterns.
I was hoping for a good replacement for Date.parse(String) too, but it looks like none exists (or at least not a built in one).
As other comments have said, it was deprecated because parsing date strings could be ambiguous depending on the format. However, that doesn't help you much if you want to be able to parse a wide variety of dates without specifying them all.
So the way I see it, you have three choices:
- make SimpleDateFormat for each format you are interested in and then try to parse it into each of them
- Make your own implementation of DateFormat that can handle every format that you're interested in
- Use Date.parse(String) (or new Date(String)) and just suppress the deprecation warnings.
Personally, for my application, I chose to specify a few format strings and if none of them were matched, I used the deprecated new Date(String) method. I don't like using deprecated methods, but I felt that as a last ditch effort to parse a date that had already failed a few other attempts, it wouldn't hurt.
精彩评论