This discussion is archived
9 Replies Latest reply: Apr 25, 2009 5:20 AM by masijade RSS

DateFormat - lenient not working as expected?

807588 Newbie
Currently Being Moderated
Given this piece of code in JDK 1.4:
SimpleDateFormat simpleDateFormat = (SimpleDateFormat)SimpleDateFormat.getDateInstance();
simpleDateFormat.applyPattern("M/d/yyyy");
simpleDateFormat.setLenient(false);
If you try this:
System.out.println(simpleDateFormat.parse("1/1/2009@"));
Why doesn't this throw a parse exception? Nevermind that to a human the @ is pure garbage and no database will ever save a date with a @ in it... We're simply writing tests to guard against bad user inputs and oddly enough that particular input is being successfully parsed...

According to the javadocs for DateFormat.setLenient(boolean):
"Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format."

So that string doesn't match that object's format...

Any ideas? Should we use the date utilities from Apache Commons instead?

Thanks!
  • 1. Re: DateFormat - lenient not working as expected?
    807588 Newbie
    Currently Being Moderated
    Check out:

    [http://forums.sun.com/thread.jspa?forumID=31&threadID=5379959|http://forums.sun.com/thread.jspa?forumID=31&threadID=5379959]
  • 2. Re: DateFormat - lenient not working as expected?
    807588 Newbie
    Currently Being Moderated
    The parse method doesn't throw a ParseException, it returns a null in case of error, and according to the documentation:

    Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

    Try the following:
    System.out.println(simpleDateFormat.parse("1/1/2009@", new ParsePosition(0)));
    System.out.println(simpleDateFormat.parse("1/1/2009@", new ParsePosition(1)));
    --janeiros                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
  • 3. Re: DateFormat - lenient not working as expected?
    807588 Newbie
    Currently Being Moderated
    Great! That's the answer I was looking for.

    Thanks!
  • 4. Re: DateFormat - lenient not working as expected?
    masijade Explorer
    Currently Being Moderated
    janeiros wrote:
    The parse method doesn't throw a ParseException,
    No? Then what is this
    public Date parse(String source)
    throws ParseException

    Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

    See the parse(String, ParsePosition) method for more information on date parsing.

    Parameters:
    source - A String whose beginning should be parsed.
    Returns:
    A Date parsed from the string.
    Throws:
    ParseException - if the beginning of the specified string cannot be parsed.
    it returns a null in case of error, and according to the documentation:

    Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

    Try the following:
    System.out.println(simpleDateFormat.parse("1/1/2009@", new ParsePosition(0)));
    System.out.println(simpleDateFormat.parse("1/1/2009@", new ParsePosition(1)));
    --janeiros
    Now, everything you've said here does apply to this parse method, but this is not the one the OP was using. Of course, the phrase about not using the entire String applies to both. Plus, this method, when using the overriden one from SimpleDateFormat, rather than the original from DateFormat, will throw an NPE if one of the arguments is null (then again, the one from DateFormat may also, but it is, at least, not documented to).

    Just a little FYI.

    Edited by: masijade. on Apr 24, 2009 6:59 PM
  • 5. Re: DateFormat - lenient not working as expected?
    807588 Newbie
    Currently Being Moderated
    Truly, I don't get your point, the class is SimpleDateFormat not Date. Besides the parse method of the Date class is a deprecated one.

    So???

    --janeiros                                                                                                                                                                                                                                                                                                                   
  • 6. Re: DateFormat - lenient not working as expected?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    janeiros wrote:
    Truly, I don't get your point, the class is SimpleDateFormat not Date. Besides the parse method of the Date class is a deprecated one.
    Read the response more carefully this time.

    The SimpleDateFormat.parse(string) method, which is actually derived from DateFormat, returns a Date.

    And that method is not deprecated.
  • 7. Re: DateFormat - lenient not working as expected?
    807588 Newbie
    Currently Being Moderated
    What are you talking about???

    This is from: http://java.sun.com/j2se/1.4.2/docs/api/java/util/Date.html
    parse(String s) 
              Deprecated. As of JDK version 1.1, replaced by DateFormat.parse(String s).
    --janeiros                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
  • 8. Re: DateFormat - lenient not working as expected?
    jschellSomeoneStoleMyAlias Expert
    Currently Being Moderated
    janeiros wrote:
    What are you talking about???
    What I am talking about is that now you have failed to read two responses correctly.

    Exactly where do you see the 'Date' class in the following signature?

    SimpleDateFormat.parse(string)
  • 9. Re: DateFormat - lenient not working as expected?
    masijade Explorer
    Currently Being Moderated
    janeiros wrote:
    Truly, I don't get your point, the class is SimpleDateFormat not Date. Besides the parse method of the Date class is a deprecated one.

    So???

    --janeiros
    As jschell has already said, "what are you talking about"? The only place in my entire post where "Date" was a single word, alone, was in the quoted documentation of the parse method of the SimpleDateFormat class which, as also already said, returns a Date, which is exactly what was being shown in that quoted documentation.

    And, it was only to correct some of the misiniformation you gave in saying that the "parse(String)" method of SimpleDateFormat does not throw a ParseException, which it does.