解析 Dateformat 时出现问题,需要更改此方法以接受空值

issues while parsing Dateformat, need to change this method to accept nulls as well

我在这里做错了什么,目前 xml 请求文件中的节点进入,值 01/22/2020 必须是 populated.testers 是 运行 新测试用例和不填充,它会抛出异常。 我怎样才能接受空字符串。

ParseException:无法解析的日期:“”。

在java.text.DateFormat.parse

           if(stringDate!=null)

而不是 if(stringDate!=null) 使用 StringUtils.isEmpty()

您还需要添加空检查。如果它是空的,则格式化程序将无法解析它。

if (stringDate != null && !stringDate.isEmpty()) {
    if (stringDate.contains("-")) {
        format = "yyyy-MM-dd";
    } else if (stringDate.contains("/")) {
        format = "MM/dd/yyyy";
    }
    SimpleDateFormat formatter = new SimpleDateFormat(format);
    return formatter.parse(stringDate);
} else {
    return null;
}