aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Validating The Date String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Validating The Date String" Watch "Validating The Date String" New topic
Author

Validating The Date String

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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.


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

does this function validate also 1999-3-14 ?
because i think it must be 1993-03-14 ...


SCJP 5, SCWCD 5, SCBCD 5
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5206
    
  12

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)


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

Joined: Nov 03, 2010
Posts: 581

Yes. Looks like I'll have to adopt one of the other solutions mentioned on that page like regular expressions.
Jonathan Elkharrat
Ranch Hand

Joined: Dec 31, 2008
Posts: 170

i used both regular expression and dateFormat..
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

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.
Oladeji Oluwasayo
Ranch Hand

Joined: Sep 10, 2010
Posts: 101

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.


OCPJP 6, OCMJD 6
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4363
    
    8

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
Bartender

Joined: Jul 19, 2004
Posts: 5206
    
  12

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

Joined: Sep 10, 2010
Posts: 101

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
Bartender

Joined: Jul 19, 2004
Posts: 5206
    
  12

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

Joined: Sep 10, 2010
Posts: 101

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

Joined: Nov 03, 2010
Posts: 581

This method below seems to be sufficient to validate dates for the assignment:

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5206
    
  12

Why are you parsing the date if it does not have the correct format?
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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

Joined: Dec 31, 2008
Posts: 170

SEAN:

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

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5206
    
  12

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating The Date String