My advice is to always disconnect. I can't think of a case where you want to be lenient, and this parameter should never have been used by default for classes like SimpleDateFormat. Lenient processing can interpret garbage as valid time strings and open errors that can be difficult to catch during testing. Also, if you use lenient to tolerate changes in the time format, you are about to be burned. For example:
System.out.println(new SimpleDateFormat("yyyyMMdd").parse("2010-12-30"));
Holds this (your time zone may vary):
Mon Nov 02 00:00:00 EST 2009
This absurd result is minus one month ("-1"), the second day ("2-") of 2010. Zero month is December!
Unfortunately, using setLenient (false) does not lead to a strict interpretation of the template. SimpleDateFormat will tolerate garbage after matching the pattern, as described here:
SimpleDateFormat.parse () ignores the number of characters in the template
In addition, it is not strict with respect to the number of character patterns, such as “d” instead of “dd”:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/d"); sdf.setLenient(false); System.out.println("For 5: " + sdf.parse("2010/01/5")); System.out.println("For 05: " + sdf.parse("2010/01/05")); System.out.println("For 15: " + sdf.parse("2010/01/15"));
Productivity:
For 5: Tue Jan 05 00:00:00 EST 2010 For 05: Tue Jan 05 00:00:00 EST 2010 For 15: Fri Jan 15 00:00:00 EST 2010
Also with setLenient (false) "2010/01/5" the template "yyyy / MM / dd" is accepted. And data disagreement is ignored, for example, "1999/2011" with the template "yyyy / yyyy" (answer - 2011).
Using SimpleDateFormat to check date / time strings is unfortunately not reliable. If you follow the link above, you will see some solutions, including a more rigorous version of SimpleDateFormat, written by me!
Steve Zobell Oct 22 '13 at 14:34 2013-10-22 14:34
source share