How to prevent FastDateFormat template "yyyy-MM-dd" from parsing a string in the format "dd-MM-yyyy"

I have two lines of format for parsing a date: "yyyy-MM-dd" and "dd-MM-yyyy", and I was hoping that the FastDateFormat class would be able to distinguish between the two so that one of them would be executed using ParseException and the other would work. However, both formats analyze the same string values, where, obviously, they are correctly parsed, and the others are not.

My test code shows:

Parsed: "2014-06-06" into Fri Jun 06 00:00:00 EDT 2014 using "yyyy-MM-dd" Parsed: "2014-06-06" into Sat Dec 05 00:00:00 EST 11 using "dd-MM-yyyy" Parsed: "06-06-2014" into Sat Dec 05 00:00:00 EST 11 using "yyyy-MM-dd" Parsed: "06-06-2014" into Fri Jun 06 00:00:00 EDT 2014 using "dd-MM-yyyy" 

Is there an easy way to get FastDateFormat to properly manage a 4-digit year based on a template? "I do not see any mitigating settings in FastDateFormat.

+5
source share
2 answers

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); } // ... more overrides of similar parse methods } 

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).

+3
source

My solution is to format the parsed Date and compare String with the original. This is more general than checking that a particular character is in a specific place, but it is also slower. It allows the user to customize the format.

 private final FastDateFormat myDateFormat = FastDateFormat.getInstance("dd-MM-yyyy"); public Date parseDate(String dateString) throws ParseException { final Date parsedDate = myDateFormat.parse(dateString); if(dateString != timeRangeDateFormat.format(parsedDate)){ throw new ParseException("Strict mode engaged", 0); } } 
0
source

Source: https://habr.com/ru/post/1203650/


All Articles