-1

I was trying to answer a question, and I prepared a little example, expecting to offer some success and error messages, after having the code working I tried:

CODE

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
// correct format in string but totally wrong data for a date.
String curDate = "2015-18-32";
Date parsedDate = null;
try {
    parsedDate = format.parse(curDate);
} catch (ParseException e) {}

System.out.println(parsedDate);

OUTPUT

Sat Jul 02 00:00:00 CEST 2016

DEMO HERE

I understand resulting date is just the valid one (2015-12-31) adding 6 months and 1 day to the date, but that does not seem correct for me. Am I misunderstanding SimpleDateFormat?

  • Shouldn't this code give an exception or error?
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
  • IIRC `SimpleDateFormat` is lenient by default, i.e. it just rolls over and adds excess days and months. – Thomas Aug 27 '15 at 10:36
  • It is lenient parsing which uses heuristics: http://docs.oracle.com/javase/7/docs/api/java/text/DateFormat.html#isLenient%28%29. This feature can be turned off by `df.setLenient(false)` – Alex Salauyou Aug 27 '15 at 10:37
  • See [this question](http://stackoverflow.com/questions/7606387/what-is-the-use-of-lenient) – Keppil Aug 27 '15 at 10:37
  • See also [How to sanity check a date in java](http://stackoverflow.com/questions/226910/how-to-sanity-check-a-date-in-java). – Raedwald Aug 27 '15 at 10:39
  • 1
    Just wondering: how much previous research have you done? – GhostCat Aug 27 '15 at 10:42
  • @Jägermeister enough.... non having english as mother language does not help.... lenient is a word i never heard, is difficult to get some info when you don't know the word are you searching. – Jordi Castilla Aug 27 '15 at 10:46

1 Answers1

4

From the documentation:

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

This actually adds 6 months and 2 days to the date with the out-of-range figures for month and day in month, ending up with July 2nd 2016.

See also jwenting's comment below on java.util.Calendar leniency.

Community
  • 1
  • 1
Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    more strictly, the underlying Calendar instance is the one that's lenient: "Calendar has two modes for interpreting the calendar fields, lenient and non-lenient. When a Calendar is in lenient mode, it accepts a wider range of calendar field values than it produces. When a Calendar recomputes calendar field values for return by get(), all of the calendar fields are normalized. For example, a lenient GregorianCalendar interprets MONTH == JANUARY, DAY_OF_MONTH == 32 as February 1. " – jwenting Aug 27 '15 at 10:41
  • @jwenting thanks, added a reference to your comment. – Mena Aug 27 '15 at 10:42
  • @jwenting and Mena, thanks, I had to translate your answer to understand lenient... :S, my problem was the english in this case, not misunderstanding the code.... ;) – Jordi Castilla Aug 27 '15 at 10:48
  • @JordiCastilla `lenitivo`? `indulgente`? or `indulgent`? :) – Mena Aug 27 '15 at 10:52
  • I knew indulgent yes, because indulgente is also a word in spanish... but lenitivo or similar does not exists for this case.... word [lenitivo](http://buscon.rae.es/drae/srv/search?val=lenitivo) has not this meaning in spanish... Check [here synonims of indulgente](http://www.wordreference.com/sinonimos/indulgente) – Jordi Castilla Aug 27 '15 at 10:55
  • @JordiCastilla aha thanks. – Mena Aug 27 '15 at 11:30