0

I recently changed... continents and temporarily forgot to signify July 27th 2023 in the European way, i.e "27/07/2023", and instead maintained the American - style literal "07/27/2023", which I promptly passed as argument to the parse method of a SimpleDateFormat instance initialized with the European pattern "dd/MM/yyyy". I would have expected a ParseException to arise since there are only 12 months in the year, but the following runs fine:

System.out.println(new SimpleDateFormat("dd/MM/yyyy").parse("07/27/2023"));

and outputs

Fri Mar 07 00:00:00 EET 2025

I'm running corretto-17.0.2 JDK.

I understand why this happens mathematically (27 = 2 * 12 + 3 and it's March of 2023 + 2 = 2025, yay!) and I also understand that SimpleDateFormat has problems of its own, e.g thread (un)safety, but I was wondering whether this is something that could be reported as a bug, or whether there is some logic behind it that I don't understand. The official Oracle docs DON'T stipulate SimpleDateFormat::parse() as deprecated or considered for removal. Appreciate any input.

Jason
  • 2,495
  • 4
  • 26
  • 37
  • 3
    from [java doc](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/text/DateFormat.html#parse(java.lang.String,java.text.ParsePosition)) `By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).` – Iłya Bursov Jul 28 '23 at 20:31
  • Interesting, but I see that this is a point made in the method `parse(String, ParsePosition)`. I myself am using the simpler `parse(String)`, whose documentation is right above the one you linked. – Jason Jul 28 '23 at 20:34
  • 2
    `See the parse(String, ParsePosition) method for more information on date parsing.` – Iłya Bursov Jul 28 '23 at 20:35
  • Ah, I think that what we've found here is a bit of lacking documentation. It turns out that even the method `parse(String)` is affected by the leniency of the parsing. I will post an answer to this referencing your comment @IłyaBursov. Thank you. – Jason Jul 28 '23 at 20:36
  • 2
    it is not lacking of documentation, first doc has link to 2nd, which implies that it basically calls "extended" version (I'd assume just set position to 0) – Iłya Bursov Jul 28 '23 at 20:37
  • 2
    Java-8 gave you the modern date-time API in March 2014. Any reason why you are still using an outdated and error-prone API? – Arvind Kumar Avinash Jul 28 '23 at 20:45
  • You may be implying it already, but I strongly recommend that you don’t use `SImpleDateFormat`. It’s notoriously troublesome and long outdated. You just stumbled on just one of its many troublesome points. Use [java.time, the modern java date and time API]()https://docs.oracle.com/javase/tutorial/datetime/index.html and its `DateTimeFormatter`. It gives you an exception as you had expected. – Ole V.V. Aug 06 '23 at 05:27
  • But to answer your questions as asked: This had been supposed to be a feature, but I agree with you, and I believe most Java programmers do: it’s rather turned out to be an anti-feature. I would go so far as to say it’s a basic error in the design. I am certain that they are not changing the design 25 years later and 10 years after the class was supplanted by java.time. The error has been abandoned already since the class has, so no need to fix it. – Ole V.V. Aug 06 '23 at 05:28
  • 1
    I completely understand and indeed, in the meantime have switched to using `DateTimeFormatter`. – Jason Aug 06 '23 at 10:15
  • If the question is "_Is this a bug in the JDK_", the answer is _almost_ always "No, it's not". – Abhijit Sarkar Aug 08 '23 at 04:52

1 Answers1

1

As mentioned by the comments, if you follow the documentation of parse(String) to the (linked) parse(String, ParsePosition), you find out that you can adjust the "leniency" of the parsing, and, if you set it to strict, as the following code does, a ParseException is thrown in this case.

SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(false);
System.out.println(sdf.parse("07/27/2023"));
Jason
  • 2,495
  • 4
  • 26
  • 37