Disable lenient parsing in DateFormType#4175
Conversation
FastDateFormat.parseObject() silently accepted invalid dates by rolling over values (e.g. month 13 became month 1 of the next year). Use SimpleDateFormat with setLenient(false) to reject invalid dates.
|
Thanks @gilisd, Good catch. Looking at the existing code, there is now the dateFormat field which gets initialized in the constructor. However, it's now only used in the other method. Isn't there anything in the FastDateFormat class that can be used to reuse the field? |
FastDateFormat does not support lenient=false. Using local SimpleDateFormat instances in both methods avoids shared state and is thread-safe. The overhead of creating a new instance per call is acceptable as these methods are not expected to be called frequently on the same object.
Good point. FastDateFormat does not support setLenient(false) — it is explicitly lenient by default, so it cannot be used for strict parsing. We addressed your concern by removing the dateFormat field entirely. Both methods now use a local SimpleDateFormat instance, which keeps the code consistent and avoids shared state. Thread-safety is preserved since local variables are per-thread. The overhead of creating a new instance per call is acceptable as these methods are not expected to be called frequently on the same object. |
|
@gilisd in order to avoid the creation of the date pattern on every conversion, can't we create 2 fields? Thread safety is fine since we are not going to change those 2 fields at any time. |
|
@gilisd, thinking a bit more about this. This PR now fully disables lenient parsing and we switch the date format from Looking at the Javadoc of
Perhaps we need to keep the |
FastDateFormat.parseObject() silently accepted invalid dates by rolling over values (e.g. month 13 became month 1 of the next year). Use SimpleDateFormat with setLenient(false) to reject invalid dates.
Check List: