• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to validate a date?

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Roel, that's it !
 
Andy Jung
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... 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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carlos Morillo wrote:My bad sorry I missed that.


Not a problem
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 221
Scala Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
reply
    Bookmark Topic Watch Topic
  • New Topic