{
Date date = null;
if (s == null)
{
throw new ParseException("Input string was null", -1);
}
if (sdf != null) // This implies dateFormats.size() > 0
{
// First test the FORMATx patterns. If any of these match, break
// the loop.
for (int i = 1 ; i < dateFormats.size() - 1; i++)
{
sdf.applyPattern((String) dateFormats.get(i));
try
{
date = sdf.parse(s);
break; // We got a matching date. Break the loop
}
catch (ParseException e)
{
// ignore
}
}
// Now test the FORMAT pattern which is the first one in the array.
// if no format but just FORMATx has been given, all of the patterns
// have been shifted "one down", e.g. tested as format2, format3, format4, format1
// in sequence.
if (date == null)
{
sdf.applyPattern((String) dateFormats.get(0));
try
{
date = sdf.parse(s);
}
catch (ParseException e)
{
// ignore
}
}
}
// Still no match. Either we had no format patterns or no pattern matched.
// See if we have a DateFormat object around. If there were patterns given
// and just none matched, that we might have date==null and df==null...
if (date == null && df != null)
{
date = df.parse(s);
}
// if the date still has not been parsed at this point, throw
// a ParseException.
if (date == null)
{
throw new ParseException("Could not parse the date", 0);
}
return date;
}