Loading...
Logo
Processing Forum
Hey,

I'd like to parse string dates from external files for use in one of my apps. I've gotten it working with simpledateformat, but as far as I can see, I need to specify the input format. As in:
Copy code
  1. SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");

My problem is that I'm not in control of the input date format, so I'm wondering if there is any way to check generically check for common formats, and see if one matches? In a similar way to excel interprets many formats.

  • Is my only other option to iterate through a long list of possible formats (that I come up with) until one returns true?
  • Alternatively, I suppose I could grep for certain 'date' characteristics (four digit number between 2000 and 2011, etc) to try to determine the format. Just seams like this is something that might already be implemented by someone much smarter than me.
  • Third option would be to prompt user to give the date format, then parse on that.

Cheers,


RT

Replies(2)

I would choose option 1 (iterate through a list of possible formats). One question is, how are you going to tell the difference between YY/mm/dd and YY/dd/mm dates?

Anyway, maybe this (external) thread about date recognition might help :
http://www.coderanch.com/t/325913/java/java/Date-recognition-String
Yeah, iterating seems like the obvious choice, was just hoping there may have already been a method defined somewhere. I agree that there are certain dates that are ambiguous.

That thread is very similar to what I'm interested in, though short on answers. One person mentions a grep solution, but doesn't give it!

Any other suggestions?

Ta,

RT