wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Question on ParseException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question on ParseException" Watch "Question on ParseException" New topic
Author

Question on ParseException

Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: May 05, 2000
Posts: 13974
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).


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
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

Joined: May 05, 2000
Posts: 13974
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

Joined: Feb 28, 2001
Posts: 455
Okie Dokie. Thanks a lot Thomas! I appreciate your help!
Jennifer Sohl
Ranch Hand

Joined: Feb 28, 2001
Posts: 455
Okie Dokie. Thanks a lot Thomas! I appreciate your help!
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
You could also use regex to do your validation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on ParseException
 
Similar Threads
Parsing date..?
problem insering date value in database
Inserting the Date in to MySQL
util.Date to sql.Date
Question about static methods...