File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes how to validate a date? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "how to validate a date?" Watch "how to validate a date?" New topic
Author

how to validate a date?

Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
hi,

when trying to validate the booking date (format yyyy/MM/dd) I use the following code snippet:



But actually this is not what I expected, because this returns a valid date as well for "2010/13/00" as an example.
So is there any method which can tell me if a given date is existent or not (considering leap years etc.)?

Kind regards,
Andy


SCJP, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

Hi Andy,

From the javadoc:
By default, parsing is lenient: If the input is not in the form used by this object's format method but can still be parsed as a date, then the parse succeeds. Clients may insist on strict adherence to the format by calling setLenient(false).


So adding following code before date parsing will solve your problem:

Kind regards,
Roel


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
Thanks Roel, that's it !
Andy Jung
Ranch Hand

Joined: Feb 07, 2010
Posts: 150
... as I stated it works fine, but there's still a thing I'm not sure about, even when using strict parsing with format "yyyy/MM/dd":
Following examples are all valid:
"10/1/01", "2010/1/1", "10/1/1" instead of what I would expect, namely : "2010/01/01"
What I do now additionally to avoid this is to check the length of the date and compare it with the length of the pattern.

But anyway is there another trick doing this automatically?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

Your last example results in validDate being false. First 2 examples are valid dates, but 2010-04-18 is not. So maybe it's time you have a look on the api of SimpleDateFormat, because you clearly don't have a correct understanding of how it works! It is not a strict parsing like when you would use a Pattern for example. So "dd" doesn't require 2 digits (indicating day), the "/" must be present (that's why the example where different fields are seperated with "," and "-" fails).

I didn't do any date validations and stored the date just as a String, because you can only update the customer id, all other values are immutable in this assignment, so why bother to try parsing this date. You could use a combination of a Pattern (\d\d\d\d/\d\d/\d\d) and the SimpleDateFormat.
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Everybody,

Is there a need to do any Date format validation in the assignment?

The only data field to be modified and updated is the customer id.

I am assuming the Date data fields are just Strings and they are correct and valid as they are.

Thanks,

Carlos.


SCSA, OCA, SCJP 5.0, SCJD http://www.linkedin.com/in/carlosamorillo
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

Carlos Morillo wrote:Is there a need to do any Date format validation in the assignment?

The only data field to be modified and updated is the customer id.

I am assuming the Date data fields are just Strings and they are correct and valid as they are.

Exactly my point in my previous post
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi Roel,

Oops!


I am just waking up and took a very quick glance to the thread ...
My bad sorry I missed that.


Thanks,


Carlos.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

Carlos Morillo wrote:My bad sorry I missed that.

Not a problem
E Choi
Greenhorn

Joined: Dec 03, 2005
Posts: 7
well, you might want to parse the data so that you can check that it's within the 48 hours of the date of availablity, at least that's what was required in URLyBird 1.2.1.

What i did was take the date from the selected reservation, convert to time that i could do calendar arithmetic on it to make sure the date was <= 48 hrs. if it wasn't then
reject the attempt to book the reservation.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5122
    
  12

Implementing the 48-hours rule is not a requirement for any SCJD assignment, you may implement it, but it is not a must
Carlos Morillo
Ranch Hand

Joined: Jun 06, 2009
Posts: 221

Hi E,

I am not implementing the 48 hours rule also like Roel.

All the hotel data records in my assignment's original database file that I got in May 2009 have
dates with a year 2003-2005, so in my view it doesn't make any sense if you use the present time
April 27th 2010 using the 48 hours rule to book a hotel room that is available in the past.

The only way it would make sense is perhaps if you have a way to generate a database file
with dates in the future from today and this is not the case since it is a MUST requirement to use
the original file provided from Sun, and since this is not a MUST requirement and for the sake of
simplicity I am not implementing it.


HTH,


Carlos.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: how to validate a date?
 
Similar Threads
How to validate a date greater than current date
Date Formatting.
related to Date
validating Date field and saving into DB?
how to convert date to a string