Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Date Validation !!!

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using parse function for date validation to parse the valid format of the date "mm-dd-yyyy". Now if I give the input date is "04-31-2008", which is a valid format but invalid date for the April month as we don't have 31st in April, parse function throws an exception but I want to catch this exception before It parses the date.In short I want to separate this two validation for Invalid Format and Invalid date. And I am unable to figure out how can I do this?

Thanks,
 
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use a DateFormat with setLenient(false) called on it.
 
Marshal
Posts: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about a regular expression to validate the format?
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried setLenient(false), but it is not working.

Thanks,
 
Campbell Ritchie
Marshal
Posts: 79642
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Throwing an Exception from 04-31-2009 for 31st April 2009 would appear to be "working correctly."
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know its working with parse function, but I want to catch this before date gets parsed.

e.g. my code is
DateFormat dateformat = new SimpleDateFormat(mm-dd-yyyy)
dateFormat.setLenient(false)
dateFormat.parse(dateStr)

parse function validates this exception of not allowing 04-31-2009, but I want to catch it before calling the parse function. I am not sure it is possible or not

Thanks,

 
Marshal
Posts: 28289
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

nis bootwala wrote:I know its working with parse function, but I want to catch this before date gets parsed.



Why? The purpose of the parse() method is to convert a String to a Date, and to throw an exception if that can't be done.
 
Rob Spoor
Sheriff
Posts: 22796
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use another form of parse that will not throw an exception:

I have used this myself to make sure that there is no "clutter" after the valid string, e.g. "04-30-2009abc":
pos.getIndex() is guaranteed to not change if there is an error, so in this case it will remain 0.
 
reply
    Bookmark Topic Watch Topic
  • New Topic