• 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

Validating The Date String

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my assignment the instructions specify that the date should match the format of yyyy/mm/dd. Here's an interesting page that describes how to validate a date.

One suggested solution which seems quite nice is the following:

Might be of use for anyone validating the date or converting the date string to a date object.
 
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
does this function validate also 1999-3-14 ?
because i think it must be 1993-03-14 ...
 
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
Date parsing is not that straight forward, certainly not when using the standard API (as already indicated in the link you provided).

Using your method is not enough for validating a given date:

You would expect false in the 2 last statements, so you'll have to combine it with a regex to valid the format and if you have a valid format, use the SimpleDateFormat to check if it can be parsed.

(I didn't do anything about parsing and just handled dates as String)
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. Looks like I'll have to adopt one of the other solutions mentioned on that page like regular expressions.
 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i used both regular expression and dateFormat..
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just used a SimpleDateFormat...though I'd probably have added the regex if I'd thought it through properly. At least there were no user-entered dates, though, so I was just validating what was already in the database.
 
Ranch Hand
Posts: 101
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

At least there were no user-entered dates, though, so I was just validating what was already in the database.



What of the "CREATE" functionality that allow users to create new records. At least you should have a field that accepts the date. You can only escape validation if you want to use three drop-down menus for the date (which to me is an over-kill, though is used it for booking records to ensure the integrity of the 48-hour rule) or do like Roel, just treat the dates as String, no validation.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Oladeji Oluwasayo wrote:What of the "CREATE" functionality that allow users to create new records.



That functionality was only required in the DB interface. And that interface treated all entries as Strings, as it had no knowledge of what the columns stood for. The business interface and user interface didn't provide a create function as that was not required.
 
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

Oladeji Oluwasayo wrote:What of the "CREATE" functionality that allow users to create new records.


When that functionality is exposed in the GUI I'll make the necessary changes to add date validation, but for the current functional requirements that's not needed (or in short: YAGNI )
 
Oladeji Oluwasayo
Ranch Hand
Posts: 101
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Taking the above instruction into consideration,

Your user interface should be designed with the expectation of future functionality enhancements, and it should establish a framework that will support this with minimal disruption to the users when this occurs.



and with the specifications in the DB interface, I've already created a fully functional BOOK, UNBOOK, CREATE, MODIFY, DELETE, SEARCH & REFRESH. And I even went a step further by creating LIVE UPDATES (search results generation as you type) and creating a checkbox menu item to enable/disable it. Thank God that the marking criteria was given, maybe I would have developed a Filthy Rich Client without knowing it won't even add to my grade. I think I've gone too far here (sorry I'm an interface freak. Very funny, I actually thought Java is a 3D animation software and thats what brought me in.), I should have been visiting this forum more often to know what is worth it and what is not. I shouldn't have added these features because I'm under a very busy schedule and now I'm feeling that its all a waste of time. As any other person gone this far?

Well, thanks champs.
 
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

Oladeji Oluwasayo wrote:As any other person gone this far?


There are people who made more than required, adding not required functionalities like unbook/create/update/delete/... or making their GUI really fancy with a lot of icons,... but in the end you have done a whole lot extra and the only thing you might get out of it, is... losing points because your solution is too complex or you have some bugs in the extra functionalities you developed. So from that point of view it's the worst time investment you could possibly do, but you might have learnt a lot from these extras you developed, making it more than worth to make this extra effort.
 
Oladeji Oluwasayo
Ranch Hand
Posts: 101
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, let me look on the bright side. I won't have to refactor (erase the extras) and moreover, I may end up with 38 out of 40 for GUI, he he he Consoling my self.
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This method below seems to be sufficient to validate dates for the assignment:

 
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
Why are you parsing the date if it does not have the correct format?
 
Sean Keane
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roel De Nijs wrote:Why are you parsing the date if it does not have the correct format?



Just for fun (and because I'm using the code is just to describe a solution, not provide production ready code ;-).

This semantically equivalent, but syntactically different solution should do it too:

 
Jonathan Elkharrat
Ranch Hand
Posts: 170
Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
SEAN:

why you got 2 return statement? i think the inner one should be in a try clause..

 
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

Jonathan Elkharrat wrote:why you got 2 return statement? i think the inner one should be in a try clause..


When you use the parse-method with a ParsePosition-instance no ParseException will be thrown when parsing fails, but null is returned. So this code is just fine.
 
I think she's lovely. It's this tiny ad that called her crazy:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic