Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on ParseException

 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question on what throws a ParseException.
I think I should be getting one, but I don't. Here's my code:

If I key a date of 11/2/02 my code executes the 'else' statement, which parses this date. Since it is neither 8 or 10 characters, it should throw a ParseException shouldn't it? It doesn't. However if I key only and '11' and try to parse it, I get a ParseException. Why don't I get it on the other one?
Thanks a million!
[ edited to adjust code format -ds ]
[ September 05, 2002: Message edited by: Dirk Schreckmann ]
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API reveals all:
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).
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I tried doing a setLenient(false) on my SimpleDateFormat objects, but still got the same result.
Am I doing something wrong?
Thanks again!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the Sun forums:
According to the documentation for SimpleDateFormat, it will pad numbers that are too short with zeros. It does not consider that to be an error. If you want more or different checking, you could write a class that extends it and includes your validations.
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okie Dokie. Thanks a lot Thomas! I appreciate your help!
 
Jennifer Sohl
Ranch Hand
Posts: 455
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okie Dokie. Thanks a lot Thomas! I appreciate your help!
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could also use regex to do your validation.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic