SimpleDateFormat problems with 2 year date

前端 未结 4 1694
名媛妹妹
名媛妹妹 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:16

    SimpleDateFormat API:

    For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits. So using the pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.

    As for lenient, when it's set to false parse throws exception for invalid dates, eg 01/32/12, while in lenient mode this date is treated as 02/01/12. SimpleDateFormat uses Calendar internally, details about leniency can be found in Calendar API.

提交回复
热议问题