解析日期格式时出现问题,需要更改此方法以也接受空值

问题描述 投票:0回答:1

我在这里做错什么,当前请求文件中的xml节点进入,必须填充值01/22/2020。测试人员正在运行新的测试用例,而不是在填充并且抛出异常。我该如何接受空字符串。

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

在java.text.DateFormat.parse

public static Date convertStringtoDate(String stringDate, String format) throws ParseException{
           if(stringDate!=null){
               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
          }
       }
   }
java spring validation methods xml-parsing
1个回答
0
投票

代替if(stringDate!= null)使用StringUtils.isEmpty()

© www.soinside.com 2019 - 2024. All rights reserved.