Android 字符串到日期(严格转换)

Android 字符串到日期(严格转换),android,simpledateformat,parseexception,Android,Simpledateformat,Parseexception,为了将字符串转换为日期,我创建了以下函数,但我需要在执行此操作时捕获任何错误,并返回null public static Date toDateStarted(String dateStarted) { Date dtStarted = null; SimpleDateFormat dtFormat = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a"); try { dtStar

为了将字符串转换为日期,我创建了以下函数,但我需要在执行此操作时捕获任何错误,并返回null

   public static Date toDateStarted(String dateStarted) {
        Date dtStarted = null;
        SimpleDateFormat dtFormat = new SimpleDateFormat("MM/dd/yyyy h:mm:ss a");
        try {
            dtStarted = dtFormat.parse(dateStarted);
        } catch (ParseException e) {
            dtStarted = null;
        }
        return dtStarted;
    }
日期dtError=toDateStarted(“错误/7/14:44:44 PM”)
这将按预期返回null

日期DtError=toDateStarted(“2016/7/1 4:44:44 PM”)
这返回了错误的日期“周二12月7日16:44:44 EST 168”

在最后一个重要案例中,输入日期“2016/7/1 4:44:44 PM”格式错误。我将通过2016年7月1日而不是2016年7月1日,因此我认为ParseException将发生,但从未发生过

尝试之后。。。dtStarted=“周二12月07日16:44:44东部标准时间168”所以我得到了一次返回

时间是正确的(16:44=下午4:44),但日期(年、月、日)完全错误

如果输入日期(DateStart)不像SimpleDataFormat模式中那样严格,我需要避免任何转换

我该怎么办?欢迎进行优化。

可能重复的可能重复的