• Post Reply Bookmark Topic Watch Topic
  • New Topic

Date Formats MM/dd/yyyy  RSS feed

 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Everyone,

I am trying to validate the date in MM/dd/yyyy format.
I use the
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");

But if I enter a date like 90/89/5678
it gets converted to m/dd/5678
(where m = single digit and dd = 2 digits number)
Instead I want to get an error that day in months should be less than
31 and months should be upto 12.
Is there a standard way of doing it ?
Say some some java util classes. Or do I have to tokenize and check it myself ?

Thanks,
Gayatri
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use formatter.setLenient(false) to force the date to be valid. Otherwise the SimpleDateFormat will use the "closest" valid date without generating an exception. After you set leniency to false, you should get a ParseException.
 
Gayatri Ganesh
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Blake.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!