Trying to debug an issue that is baffling me. Is there any scenario where the below code could return a string "20200931" (there are only 30 days in September). I don't think a java date can hold an invalid date no matter how it is constructed correct? I am seeing this invalid date string in a database table. The date is coming from a call to a soap service which returns a string in format "2020-09-01-05:00" and then converts it to a java.util.Date object. Finally it uses the below function to convert back into a string and writes to our database table. Regardless if the soap service returns the correct date or not I dont see how the dateFormat function can return "20200931".
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
...
private static final DateFormat PERIOD_DATE_FORMAT = new SimpleDateFormat("yyyyMMdd");
public static String formatDate(Date dt) {
return dt == null? "" : PERIOD_DATE_FORMAT.format(dt) ;
}