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.
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.)?
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:
... 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?
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.
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.
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.