This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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?
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.
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?
Joined: Oct 13, 2005
The details about "lenient" are in the Calendar class API.
Is there any thing like setLenient property which I can set to false in java.util.Date and java.util.Calendar?
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.