Possible incoherence/errors on date/time parsing code
While working on other things, I've noticed this method on DateUtils:
public static Date parseDateTime (String str) {
DateFields fields = new DateFields();
int i = str.indexOf("T");
if (i != -1) {
if (!parseDate(str.substring(0, i), fields) || !parseTime(str.substring(i + 1), fields)) {
return null;
}
} else {
if (!parseDate(str, fields)) {
return null;
}
}
return getDate(fields);
}
Manually parsing date/times tends to be a pain and a common bug source in my experience and I thought that maybe we could get rid of maintaining that by simply using Joda time, which we're currently depending on for other stuff as well.
I changed the method to this:
public static Date parseDateTime (String str) {
try {
return DateTime.parse(str).toDate();
} catch(IllegalArgumentException e) {
throw new XPathTypeMismatchException(e.getMessage());
}
}
And then tests start to fail. If our parsing code was OK, replacing it with some other supposedly OK code would make no difference. IMHO, this is very fishy and it could be a sign that our current date/time parsing code has bugs. I'm not saying that Joda time is 100% reliable but I think it's reasonable to assume that they have expended much more time checking this kind of operations than us with JR.