So when trying to replace some legacy code using SimpleDateFormat and Date, to use java.time.DateTimeFormatter and LocalDate I ran into a problem. The two date formats are not equivalent. At this point I must say I know the two date types are not the same but the scenario I am in means I never care about the time aspect so can ignore it.
public Date getDate(String value) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
return dateFormat.parse(value);
} catch (ParseException e) {
return null;
}
}
public LocalDate getLocalDate(String value) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
try {
return LocalDate.parse(value, formatter);
} catch (DateTimeParseException e) {
return null;
}
}
public void testDates() {
getDate("03/07/2016"); // Sun Jul 03 00:00:00 BST 2016
getDate("3/7/2016"); // Sun Jul 03 00:00:00 BST 2016
getDate("3/7/2016 00:00:00"); // Sun Jul 03 00:00:00 BST 2016
getDate("3/7/2016 00:00:00.0+0100"); // Sun Jul 03 00:00:00 BST 2016
getDate("3/7/2016T00:00:00.0+0100"); // Sun Jul 03 00:00:00 BST 2016
getLocalDate("03/07/2016"); // 2016-07-03
getLocalDate("3/7/2016"); // null
getLocalDate("3/7/2016 00:00:00"); // null
getLocalDate("3/7/2016 00:00:00.0+0100"); // null
getLocalDate("3/7/2016T00:00:00.0+0100"); // null
}
As you can see when the same pattern is used in both formatters the DateTimeFormatter ends up producing nulls where you'd expect to see dates equivalent to that of SDF. In this scenario I would expect the unrequired data to be dropped but it isn't.
So, how do we create a robust date/time parser?!