23/12/2013 is mapping with MM/dd/yyyy format why, why not ParseException

后端 未结 5 552
野性不改
野性不改 2021-01-28 10:20

Can anybody help?

public void dateCalender() throws ParseException{
        System.out.println(new SimpleDateFormat(\"MM/dd/yyyy\", Locale.ENGLISH).parse(\"120/1         


        
5条回答
  •  不思量自难忘°
    2021-01-28 10:51

    According to docs , parsing is lenient, So you didn't get exception for invalid input. It converted to another value.

    By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).


    If you want to get java.text.ParseException: Unparseable date:, than apply setLenient(false); at SimpleDateFormat

        String dateStr="120/12/2013";
        SimpleDateFormat sdf=new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
        sdf.setLenient(false);
        try {
            Date d=sdf.parse(dateStr);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    

提交回复
热议问题