日期解析不会因格式无效而失败

Date parsing not failing with invalid format

根据我的理解,下面的一段代码应该会抛出解析异常,但实际上并没有。查看文档但无法弄清楚。

DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd", Locale.US);
dateFormat.setLenient(false);
dateFormat.parse("20160821_$folder$");

javadoc 是这么说的:

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

只要找到匹配项,它就会停止进一步扫描,这里似乎就是这种情况。

如果要严格检查,可以在此基础上添加 RegEx 数学运算,以防止此类字符串被解析。