Asha
Asha

Reputation: 11232

Generic way to parse dates in Java

What is the best way to parse dates in Java ? Is there any built in way to parse strings such as "18 Jul 2011" , "Jul 18, 2011", "18-07-2011", "2011-07-18" without knowing the format beforehand?

Upvotes: 6

Views: 7727

Answers (7)

Michele
Michele

Reputation: 6131

try DateFormat with the default Format DateFormat.MEDIUM and SHORT.

public static void main(String[] args) {
    // Make a String that has a date in it, with MEDIUM date format
    // and SHORT time format.
    String dateString = "Nov 4, 2003 8:14 PM";

    // Get the default MEDIUM/SHORT DateFormat
    DateFormat format =
        DateFormat.getDateTimeInstance(
        DateFormat.MEDIUM, DateFormat.SHORT);

    // Parse the date
    try {
        Date date = format.parse(dateString);
        System.out.println("Original string: " + dateString);
        System.out.println("Parsed date    : " +
             date.toString());
    }
    catch(ParseException pe) {
        System.out.println("ERROR: could not parse date in string \"" +
            dateString + "\"");
    }

Snipped from http://javatechniques.com/blog/dateformat-and-simpledateformat-examples/

On exception you have to decide what todo next, you can build a hierarchy of Parsing Trys. like:

SimpleDateFormat df1 = new SimpleDateFormat( "dd/MM/yyyy" );
SimpleDateFormat df2 = new SimpleDateFormat( "dd-MM-yyyy" );

df1.parse(..)
df2.parse(..)

and so on.

Upvotes: 2

Thomas
Thomas

Reputation: 88707

You can use DateFormat.parse(...) and use a locale for the dateformat, but that still might not handle all those cases. It's also not that easy: consider the following two possible dates (which are the same): 18/07/2011 and 07/18/2011 - how should that be parsed without knowing whether it is dd/mm/yyyy or mm/dd/yyyy (e.g. in Saudi Arabia)?

Upvotes: 0

Michael Borgwardt
Michael Borgwardt

Reputation: 346327

There's nothing like that in the standard API library. Natty is a library that attempts this, but you should be aware that this can only ever be a "best effort" affair, as things like 2/1/2012 are simply ambiguous: without metainformation about the format, it's impossible to decide whether it's Feburary 1st or January 2nd.

Upvotes: 8

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16142

This is a large issue, mostly because dates are a fuzzy concept - for example is 02-01-2012 in February or January?

The best you can do is collect a bunch of different format strings and try each one until you get a match...

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

Not really. BalusC wrote a brilliant post on DateUtil, that "solves" most date formats parsing.

Upvotes: 2

Martin Algesten
Martin Algesten

Reputation: 13620

You can't really do that. Consider "10-12-2011". Do you mean 10 of december or 12 of october? You need to make your own list of patterns to try out. This list will vary depending on locale and what is the "expected" way someone in that country/locale will interpret the date.

Upvotes: 0

leifg
leifg

Reputation: 9008

If you are familiar with the Simpledateformat you can just add a couple of dateformatter in a list/array and do them all after another until one hits a valid date.

The problem here is: What do you do with a date such as

06-07-2011

Is it July 6th or is it June 7th?

But to answer to question: no there is not a "built in generic way"

Upvotes: 1

Related Questions