tags:

views:

571

answers:

14

I have a string that contains the representation of a date. It looks like:

Thu Nov 30 19:00:00 EST 2006

I'm trying to create a Date object using SimpleDateFormat and have 2 problems.

1.) I can't figure out the pattern to hard-code the solution into the SimpleDateFormat constructor

2.) I can't find a way I could parse the string using API to determine the pattern so I could reuse this for different patterns of date output

If anyone knows a solution using API or a custom solution I would greatly appreciate it.

+2  A: 

I'm not sure there's any easy way to parse a date and work out its pattern, but I would have thought that the pattern for the one you posted would be:

EEE MMM dd HH:mm:ss zzz yyyy

alexmcchessers
A: 

Are you just asking for the pattern for that given date? If so, I think this should do it:

"EEE MMM d HH:mm:ss z yyyy"

Or are you trying to take any formatted date, and infer the format, and parse it?

pkaeding
+7  A: 

The format to pass to SimpleDateFormat could be looked up at http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html

new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")

As for your second question, I don't know of any Java library to figure out a date format and parse it without knowing in advance what the format is.

Eli Courtwright
A: 

@alexmcchessers: Thanks! That pattern works, the best I was able to find using google was "ddd MMM dd hh:mm:ss 'EST' yyyy" which was throwing a ParseException. Hopefully there is also a way to programmatically determine the pattern though.

Kamikaze Mercenary
+1  A: 

As others have said, the pattern looks like it should be

new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"

As for parsing an arbitrary format date, I'm not aware of any library which does this in Java. If you were keen to develop such a thing, I would start by looking at the perl str2time function.

Matt Sheppard
A: 

How about:

EEE MMM dd HH:mm:ss zzz yyyy

Just pass the string into the constructor of SimpleDateFormat. To use the object, just call the parse method passing in the string you want converted to a Date.

You could take a look at:

http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html

Nick Pierpoint
+2  A: 

If you want to do anything other than parse or format a date there is not much out there for handling the patterns themselves. Sometime ago I was writing a Swing component for entering dates into a formatted text field. You supplied a pattern and it moved the text entry cursor through the elements of that pattern, only allowing valid values.

As part of that I wrote a DateFormatParser available here as part of the OpenHarmonise open source project.

Parsing a date into a pattern would be an extremely interesting problem to tackle. You would have to make certain assumptions (e.g. use of : in time not date) but you would face the eternal problems of 2 digit years and day/month or month/day arrangements.

Matt Large
A: 

This isn't really the same, but you might want to look at something like JChronic, which can do natural language processing on dates. So, the input date could be something like "tomorrow", or "two weeks from next tuesday".

This may not help at all for your application, but then again, it might.

pkaeding
A: 

I think a bigger or better question might be, WHY do you need to programmatically determine the pattern to use?

I would think that would be 1) pretty hard, 2) might not account for all cases of dates and patterns and 3) might be covering up for a design problem or hack if you need to do this in the first place.

matt b
+1  A: 

I must say i find the other question very interesting. There is one serious problem though - parse this: 08/07/06! If you limit yourself on a subset of expected formats, you could probably solve problem by playing around with regexps, you could build up bunch of expected patterns, and then break Strings on spaces or whatever, and match part by part.

Slartibartfast
A: 

@Slartibartfast: I agree that there are definitely ambiguities - this is the most common problem with dates. However, one solution could be to pass the programmatic method a default pattern like mm-dd-yy for it to assume in the case it encounters an ambiguous date. Providing no default (null) for that parameter could cause it to fail by throwing an exception, etc. This isn't a perfect solution, but it would be reasonable enough to use in most situations.

Kamikaze Mercenary
+1  A: 

It's worth knowing that the date format you have given is not an arbitrary one. It is the output of the built-in Date.toString() method (at least in the UK and US locales). Not coincidentally, it is also the format of the unix 'date' command (at least on linux, and I believe in other implementations too) - though to be pedantic, Date.toString() pads one-digit day numbers with a zero while unix date does not.

What this means is that you are likely to receive this input format when you output an unformatted date into a user-modifiable field (e.g. an HTML INPUT field) and receive it back unmodified. So just because input is coming in this format, doesn't mean the users will type in a thousand other arbitrary formats.

Of course, they still might. The way I handle date inputs in general is with a bunch of try/catch blocks, where I try it against one format, then another, then another. Our standard framework is now up to about 20 different formats by default. Of course it is still not perfect; I found someone the other day entering "03 Sept" as the date (a non-standard month abbreviation, and with no year) and we hadn't handled that scenario.

Leigh Caldwell
+2  A: 

See Apache Commons' DateUtils. There's a parseDate method that takes your String and multiple patterns to try and spits out a Date instance.

Joe Liversedge
+1  A: 

The POJava date parser org.pojava.datetime.DateTime is an immutable, and robust parser that supports multiple languages, time zones, and formats.

Best of all, the parser is heuristic and does not require a pre-existing “format” to work. You just pass it a date/date-time text string and get out a java.util.Date!

yinyueyouge