• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date validation in JAVA

 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI...

I want to validate Date based on what month user has entered

e.g if the month is April then day part of date should not be grater than 30 and same as for other months including February where we are taking care of leap year. I am using SimpleDateFormat function of DateFormat class to check the valid format of mm/dd/yyyy. But I don't understand how can I separate my valid date validation from this format validation?

Thanks,
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look through the Calendar class and its subclasses. There is something about "lenient" which you can set to false. The details should be in the API. Also Google for Apache Commons; there might be classes for calendars there, which might be easier to use than the Sun class.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're looking for an alternative to the Date and Calendar, checkout the excellent JodaTime API.

Similar API is about to be part of Java 7.
 
Maria Laxmi
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi...

Is there any thing like setLenient property which I can set to false in java.util.Date and java.util.Calendar?

I am still facing the problem with my date validation. I am using Date or Calendar object with DateFormat object, Where after deciding the date format (e.g. mm/dd/yyyy) I am using that date with Date or Calendar object and if i give the Invalid date (e.g. 04/31/2009) it automatically carry forwards the date to (05/01/2009) at time of creating new Date or Calendar object using Invalid date, and I want to catch that exception separately.

I hope it makes any sense?


Thanks,
 
Campbell Ritchie
Sheriff
Pie
Posts: 49382
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The details about "lenient" are in the Calendar class API.
 
Pankaj Rathod
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nis bootwala wrote:Hi...

Is there any thing like setLenient property which I can set to false in java.util.Date and java.util.Calendar?

Thanks,


Yes, there is. Go through the methods of the Calendar class carefully in the API (link provided by Campbell in earlier post). You will find a method 'void setLenient(boolean)' which you can use to set Calendar to non-lenient mode.

Using this, 04/31 will not be converted to 05/01 rather an Exception will be thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic