I have a date list to which I would like to validate, I have a function that works only with a single date, but I now want a date list.
Rules:
1) When the list is empty, I return true
2) When a date is invalid, I switch to the following and delete it from the list of dates
3) Edit When all the execution is finished I return true (if at least one is valid) or false (all of them failed the test).
Edit: instead of having this isDateValid(String date)
==> isDateValid(List<LString> date)
List of date:
List<String> dateList= new ArrayList<>();
dateList.add("2016-10-02T04:00:00.000Z");
dateList.add("2017-02-15T14:32:32");
dateList.add("2017-01-23");
Function (only one date):
public boolean isDateValid(String date ) {
List<SimpleDateFormat> knownPatterns = new ArrayList<>();
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd"));
knownPatterns.add(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
knownPatterns.add(new SimpleDateFormat("MM/dd/yyyy"));
for (SimpleDateFormat pattern : knownPatterns) {
try {
Date timestamp = pattern.parse(date);
return true;
} catch (ParseException e) {
continue;
}
}
return false;
}