aspose file tools*
The moose likes Beginning Java and the fly likes Date validation in JAVA Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Date validation in JAVA" Watch "Date validation in JAVA" New topic
Author

Date validation in JAVA

Maria Laxmi
Ranch Hand

Joined: Aug 08, 2008
Posts: 40
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
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

Joined: Oct 08, 2006
Posts: 2458
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

Joined: Aug 08, 2008
Posts: 40
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

Joined: Oct 13, 2005
Posts: 38851
    
  23
The details about "lenient" are in the Calendar class API.
Pankaj Rathod
Greenhorn

Joined: Feb 02, 2009
Posts: 7
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Date validation in JAVA