This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Parsing of dates in general format? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parsing of dates in general format?" Watch "Parsing of dates in general format?" New topic
Author

Parsing of dates in general format?

Bill Pugh
Greenhorn

Joined: Jul 10, 2003
Posts: 12
Does anyone have suggestions for parsing of dates in general/unspecified formats? In other words, I want something that can parse either "6/14/2005" or "June 14, 2005" to the same Date value. If I have a specific Date format in mind, I can use DateFormat.parse. But if I don't want to require a specific Date format, the only thing I'm aware of is Date.parse, and that is deprecated.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could use regular expressions to get date from a string and set a date with the results. This gives you more flexability as to what formats you can match, and stops you running into ParseExceptions. However, if you don't restrict the formats your application can support, you will always run into problems. For example 6/7/05 - is that the 6th of July 2005 or the 7th of June 2005?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
I generally avoid deprecated methods, but the Date object is an exception. It's got a number of methods that I find easier to use than the corresponding Calendar methods. And it's not like the deprecation is due to actual functional problems (like Streams vs. Readers).
If the warning really bothers you, you could lift the method from the JDK source and put it into your utilities library (not an option if you want to redistribute it, of course - check the JDK source license).


Ping & DNS - updated with new look and Ping home screen widget
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parsing of dates in general format?
 
Similar Threads
Parsing dates entered in different formats
changing the color?
using date as condition
Formatting Dates
Parsing dates entered in different formats