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

后端 未结 5 555
野性不改
野性不改 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:44

    On the first two:

    MM/dd/yyyy 120/12/2013
    

    The compiler will take this as '12/12/2013 + 118 months' and essentially solve for the correct date. In your example, it comes out as December 12, 2022 (12/12/2013 + 9 years).

    MM/dd/yyyy 23/12/2013
    

    The exact same thing happens. You get '12/12/2013 + 9 months', or 11/12/2014.

    The third one isn't technically in the MM/dd/yyyy format. As from the other answers, you can do something like this:

    SystemDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy", Locale.ENGLISH);
    sdf.setLenient(true);
    sdf.parse("Jan/12/2013");
    System.out.println(sdf.toString());
    

提交回复
热议问题