java - Lenient SimpleDateFormat acting strange -


i understand that, in order validate date strings, 1 must make dateformat instances non-lenient parseexceptions malformed dates. consider

string  dubiousdate = "2014-04-01"; dateformat  sdf = new simpledateformat( "yyyymmdd"); date d; try {     d = sdf.parse( dubiousdate);     system.out.println( dubiousdate + " -> " + d); } catch ( parseexception e) {     e.printstacktrace();     system.err.println( dubiousdate + " failed"); } 

this give

2014-04-01 -> wed dec 04 00:00:00 cet 2013

now can understand lenient calendars try nice , accept funny negative numbers, interpretation looks -01 considered month, though appears last, days are. , -04 months become 04 days, minus ignored.

in leniency, why make sense anyone?

i see possible interpretation:

in pattern yyyymmdd month part limited exact 2 chars because there no separators between different numerical fields. "-0" seen month 0 , 1 month behind january yielding december in previous year.

after having "parsed" fake month, day part comes "4" stopping before second minus char. result fourth of december.

finally, remaining chars "-01" ignored. typical class simpledateformat how handle non-digit trailing chars, example see code:

string dubiousdate = "2014-04-01xyz"; dateformat sdf = new simpledateformat("yyyy-mm-dd"); date d; try {     d = sdf.parse(dubiousdate);     system.out.println(dubiousdate + " -> " + d);     // output: tue apr 01 00:00:00 cest 2014 } catch (parseexception e) {     e.printstacktrace();     system.err.println(dubiousdate + " failed"); } 

as thumb rule, 2 equal symbol chars mm or dd parser consume @ 2 chars (if digits found).

some research java 8:

datetimeformatterbuilder builder = new datetimeformatterbuilder(); builder.parselenient(); builder.append(datetimeformatter.ofpattern("yyyymmdd")); datetimeformatter dtf = builder.toformatter(); string dubiousdate = "2014-04-01"; localdate date = localdate.parse(dubiousdate, dtf); system.out.println(date); 

according jdk-8-documentation formatter constructed way should behave leniently, unfortunately still throws exception:

"exception in thread "main" java.time.format.datetimeparseexception: text '2014-04-01' not parsed @ index 3"

best option in lenient case - theoretically - if parser ignores minus chars. not possible jsr-310 (still strict). well, simpledateformat lenient, in rather wrong way.


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -