When I come across invalid dates, i.e. 2018-02-31, 2018-11-31, I want my code to convert it to last day of that month.
I am not sure how to check for value within the passed string.
Here is my code so far:
/**
* If date comes back as invalid, i.e. 2018-11-31
* convert it to have last day of given month.
*
* @param nextFieldTypeDate
* @return
*/
public static LocalDate resolveInvalidDate(String nextFieldTypeDate) {
LocalDate convertedDate = null;
try {
convertedDate = LocalDate.parse(nextFieldTypeDate);
} catch(DateTimeException dte) {
//convertedDate = convert date to have last day of the month
DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM");
String yearMonthString = nextFieldTypeDate.subSequence(0, 7).toString();
YearMonth ym = YearMonth.parse(yearMonthString, fmt);
convertedDate = ym.atEndOfMonth();
} catch(Exception e) {
logger.error(e.getMessage());
throw new ConversionException("Unable to convert nextFieldTypeDate.", e);
}
return convertedDate;
}