I can reproduce your results using Apache-Common-Lang-library . It seems that the API does not offer an official solution, also not in the latest version v3.3.2. Typically, a good parser rejects the entry 2014-06-06 for the dd-MM-yyyy template, throwing an exception, but here FastDateFormat allows it and cannot even be set to non-condescending mode, such as SimpleDateFormat .
So, the only remaining options are:
a) Make your own hack (similar to the following code example):
public class ParserDDMMYYYY extends FastDateFormat { public static final INSTANCE = new ParserDDMMYYYY("dd-MM-yyyy", TimeZone.getDefault(), Locale.getDefault()); @Override public Date parse(String input) throws ParseException { if (input.charAt(4) == '-') { throw new ParseException("Invalid format: " + input, 0); } return super.parse(input); }
The dd-MM-yyyy prevention case for the yyyy-MM-dd template is very similar.
b) Or you change the date-time-library , because there are at least three improved libraries to process and format the date and time. Keep in mind that the apache library is still based on the old java.util.* - and java.text.* -Packs.
I also doubt that the FastDateFormat class FastDateFormat really much better in performance, certainly not better compared to immutable versions of other time libraries. For example, I saw some synchronized keywords in the apache library (potential lock competition, not so modern).
source share