SimpleDateFormat parse(string str) doesn't throw an exception when str = 2011/12/12aaaaaaaaa?

前端 未结 7 1840
野的像风
野的像风 2020-11-27 08:06

Here is an example:

public MyDate() throws ParseException {
    SimpleDateFormat sdf = new SimpleDateFormat(\"yyyy/MM/d\");
    sdf.setLenient(false);
    St         


        
7条回答
  •  暖寄归人
    2020-11-27 08:38

    Take a look on the method documentation which says: ParseException if the beginning of the specified string cannot be parsed.

    Method source code with 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.
     * 

    * See the {@link #parse(String, ParsePosition)} method for more information * on date parsing. * * @param source A String whose beginning should be parsed. * @return A Date parsed from the string. * @exception ParseException if the beginning of the specified string * cannot be parsed. */ public Date parse(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Date result = parse(source, pos); if (pos.index == 0) throw new ParseException("Unparseable date: \"" + source + "\"" , pos.errorIndex); return result; }

提交回复
热议问题