<p>I have a string that contains the representation of a date. It looks like: </p>

<p><strong>Thu Nov 30 19:00:00 EST 2006</strong></p>

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

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

<p>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 </p>

<p>If anyone knows a solution using API or a custom solution I would greatly appreciate it.</p>

[{'answer_id': 43849, 'author': 'alexmcchessers', 'author_id': 998, 'author_profile': 'https://Stackoverflow.com/users/998', 'pm_score': 2, 'selected': False, 'text': "<p>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:</p>\n\n<p>EEE MMM dd HH:mm:ss zzz yyyy</p>\n"}, {'answer_id': 43850, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 0, 'selected': False, 'text': '<p>Are you just asking for the pattern for that given date?  If so, I think this should do it:</p>\n\n<pre><code>"EEE MMM d HH:mm:ss z yyyy"\n</code></pre>\n\n<p>Or are you trying to take any formatted date, and infer the format, and parse it?</p>\n'}, {'answer_id': 43852, 'author': 'Eli Courtwright', 'author_id': 1694, 'author_profile': 'https://Stackoverflow.com/users/1694', 'pm_score': 4, 'selected': True, 'text': '<p>The format to pass to SimpleDateFormat could be looked up at <a href="http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html" rel="noreferrer">http://java.sun.com/javase/6/docs/api/java/text/SimpleDateFormat.html</a></p>\n\n<pre><code>new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy")\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 43867, 'author': 'Matt Sheppard', 'author_id': 797, 'author_profile': 'https://Stackoverflow.com/users/797', 'pm_score': 1, 'selected': False, 'text': '<p>As others have said, the pattern looks like it should be</p>\n\n<pre><code>new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy"\n</code></pre>\n\n<p>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 <a href="http://search.cpan.org/~rse/lcwa-1.0.0/lib/lwp/lib/HTTP/Date.pm" rel="nofollow noreferrer">str2time</a> function.</p>\n'}, {'answer_id': 43869, 'author': 'Nick Pierpoint', 'author_id': 4003, 'author_profile': 'https://Stackoverflow.com/users/4003', 'pm_score': 0, 'selected': False, 'text': '<p>How about:</p>\n\n<p><code>EEE MMM dd HH:mm:ss zzz yyyy</code></p>\n\n<p>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.</p>\n\n<p>You could take a look at:</p>\n\n<p><a href="http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html" rel="nofollow noreferrer">http://java.sun.com/j2se/1.4.2/docs/api/java/text/SimpleDateFormat.html</a></p>\n'}, {'answer_id': 43884, 'author': 'Matt Large', 'author_id': 2978, 'author_profile': 'https://Stackoverflow.com/users/2978', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>As part of that I wrote a DateFormatParser <a href="http://openharmonise.cvs.sourceforge.net/openharmonise/harmonise3/harmonise-gui/src/java/org/openharmonise/swing/datefield/" rel="nofollow noreferrer">available here as part of the OpenHarmonise open source project</a>.</p>\n\n<p>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.</p>\n'}, {'answer_id': 43913, 'author': 'pkaeding', 'author_id': 4257, 'author_profile': 'https://Stackoverflow.com/users/4257', 'pm_score': 0, 'selected': False, 'text': '<p>This isn\'t really the same, but you might want to look at something like <a href="https://github.com/samtingleff/jchronic" rel="nofollow noreferrer">JChronic</a>, which can do natural language processing on dates.  So, the input date could be something like "tomorrow", or "two weeks from next tuesday".  </p>\n\n<p>This may not help at all for your application, but then again, it might.</p>\n'}, {'answer_id': 44816, 'author': 'Slartibartfast', 'author_id': 4433, 'author_profile': 'https://Stackoverflow.com/users/4433', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 45035, 'author': 'Leigh Caldwell', 'author_id': 3267, 'author_profile': 'https://Stackoverflow.com/users/3267', 'pm_score': 2, 'selected': False, 'text': '<p>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.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n'}, {'answer_id': 45167, 'author': 'Joe Liversedge', 'author_id': 4552, 'author_profile': 'https://Stackoverflow.com/users/4552', 'pm_score': 2, 'selected': False, 'text': '<p>See Apache Commons\' <a href="http://commons.apache.org/lang/api-release/org/apache/commons/lang/time/DateUtils.html" rel="nofollow noreferrer">DateUtils</a>. There\'s a <code>parseDate</code> method that takes your <code>String</code> and multiple patterns to try and spits out a <code>Date</code> instance.</p>\n'}, {'answer_id': 1102356, 'author': 'yinyueyouge', 'author_id': 80347, 'author_profile': 'https://Stackoverflow.com/users/80347', 'pm_score': 2, 'selected': False, 'text': '<p>The POJava date parser org.pojava.datetime.DateTime is an immutable, and robust parser that supports multiple languages, time zones, and formats. </p>\n\n<p>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!  </p>\n'}]
['https://Stackoverflow.com/questions/43842', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2628/']