0

I need to check the string against all possible dateFormats and need to check its valid date or not.

The problem I am facing is I can't predict the date format as its getting from some other web sources. One example is September 21 2012 1255 PM ET, .But the format may vary.

user1618820
  • 109
  • 1
  • 2
  • 11

1 Answers1

1

I suggest that you implement your own Parse method and use regular expressions to determine how the string should be parsed. You should not expect to parse a date with any format without knowing which it is.

Personally I would implement something like this (but then again, I haven´t coded Java in quite some time):

public SimpleDateFormat ParseDate(string s)
{
SimpleDateFormat parsedDate;
if(s.matches("0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d"))
   parsedDate = new SimpleDateFormat("MM/dd/yyyy");
else if(s.matches([PATTERN2]))
   parsedDate = new SimpleDateFormat([DATE PATTERN 2]);
...
return parsedDate;
}

and so on...

Suggested reading:

http://docs.oracle.com/javase/tutorial/essential/regex/
http://docs.oracle.com/javase/6/docs/api/java/text/SimpleDateFormat.html

Mu Mind
  • 10,935
  • 4
  • 38
  • 69
Marcus
  • 8,230
  • 11
  • 61
  • 88