• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Validation problem  RSS feed

 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all-
I�m trying to parse a date using a SimpleDateFormatter.

private static final DateFormat tsFormatter
= new SimpleDateFormat("yyyyMMddHHmmssSS");

The problem is that this works fine for formatting but lets invalid dates slip thru:

e.g. Feb 31

Does anyone know how to catch these kind of errors?

Thank you
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you have YY MM DD isolated, use these to construct an Instance of Date or GregorianCalendar. These will throw an Exception when they're not fit to create a valid date.

J.
 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can make Calendars lenient or non-lenient. If they're non-lenient, they'll throw exceptions when bad dates are thrown in. So make a Calendar, set its leniency to false, and try to set its date with the date you've parsed.
[ May 03, 2005: Message edited by: Adam Vinueza ]
 
Max Tomlinson
Ranch Hand
Posts: 365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the quick replies
Max
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!