Sunil Kumar wrote:Use simple date format like this
There are two improvements that can be made:
- turn off leniency
- check if the parsing did not ignore the trailing part of the
string (ok, you're actually doing that at the end, but it can be a bit more efficient ;))
If the string could not be parsed, then pos.getIndex() will still return 0. If it could be parsed but there is trailing garbage (e.g. "2009-01-13 09:34:00 Hello world!") then pos.getIndex() will be smaller than dateStr.length().
As for no exception, that's because DateFormat.parse(String) uses the same technique:
And if you can avoid an exception,
you should always do so.