How Would You Programmatically Create a Pattern from a Date that is Stored in a String?

StackOverflow https://stackoverflow.com/questions/43842

  •  09-06-2019
  •  | 
  •  

Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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!

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

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.

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.

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

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.

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.

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?

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top