SimpleDateFormat problems with 2 year date

前端 未结 4 1692
名媛妹妹
名媛妹妹 2020-12-21 02:59

I\'m trying to understand two things:

  1. Why doesn\'t the following code throw an exception (since the SimpleDateFormat is not lenient)
  2. It d
4条回答
  •  时光取名叫无心
    2020-12-21 03:27

    According to the SimpleDateFormat javadoc for JDK 1.6,

    For parsing, the number of pattern letters is ignored unless it's needed to separate two adjacent fields.

    A look at the source code for the method that does the work, SimpleDateFormat.parse(String, ParsePosition), confirms this. There is a variable obeyCount that is true if the pattern has no delimiters, like "yyyyMMdd", and false otherwise. With your pattern the parser looks for 3 numbers separated by 2 delimiters and does not care about the number of digits in each position.

    The answers to your questions:

    1. Lenient is not a factor when delimiters are used.
    2. You did not call SimpleDateFormat.set2DigitYearStart so why should the code do what you didn't tell it to do?

提交回复
热议问题