• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

Date validation in JAVA

 
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 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,
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The details about "lenient" are in the Calendar class API.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic