File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Date Validation Against Multiple Formats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Date Validation Against Multiple Formats" Watch "Date Validation Against Multiple Formats" New topic
Author

Date Validation Against Multiple Formats

Tarun Trehan
Ranch Hand

Joined: Mar 16, 2012
Posts: 49

Hi All,

I am developing a program to validate date formats and values against a set of input formats.

Problem Statement:

  • User Enters a String
  • Validate Against Set of Pre-Defined Formats
  • Return boolean flag stating validation status


  • Solution :

    1. Try to parse the date.
    2. Catch parse exception to test if validation failed.
    3. Loop the above process for the set of formats against which the validation needs to be performed.


    Need your review to check if this is a good option to check for multiple date formats ??




    Thanks,
    Tarun Trehan (http://allzhere.in)
    Paul Clapham
    Bartender

    Joined: Oct 14, 2005
    Posts: 18570
        
        8

    I expect the code is sufficient to implement those requirements. My concern would be with the requirements themselves.

    So, you put "04-07-2008" through that code and the code tells you it's valid. Now what? Does it mean April 07 or does it mean 04 July?
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Paul,

    Appreciate your prompt response.

    This is a utility for a tool that needs to validate dates from an external data source.
    These are some initial requirements and later i will give an option to validate against a given format/formats.

    However, there is a possibility that the called program does not receive any format string as input.
    In that case there will be generic validation against pre-defined list of formats.

    I agree to the point you are making with "04-07-2008".
    In that case, we will have to settle with the first successful format check.
    It can be buggy but we'll live with it.

    Your thoughts ??
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    I can't speak for Paul, but my thoughts are that I would rather have it fail completely on an ambiguous format that just silently guess and go on processing as if that guess were correct. If and when that happens, you contact someone in charge of the source of that data, and ask them to pick one format to stick to, and let you know what it is. And do it over email, not on the phone. That way, you get correct behavior from then on, and your backside is covered in case something changes.

    But you know your environment, requirements, constraints, and use cases better than anyone here, so it of course has to be your call.
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Thanks Paul.

    I think, i will write my program in such a way that it returns a Custom Object. It will contain a list of formats for which validation was successful. A flag in case validation was successful for more than 1 formats.
    Will leave it on the program caller's decision to handle specifically.

    Thanks again .
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19693
        
      20

    Your loop can be a bit more efficient:
    1) you can reuse and modify one single SimpleDateFormat object.
    2) you can prevent the ParseException using the often overlooked other parse method, that takes a ParsePosition.



    SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
    How To Ask Questions How To Answer Questions
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Hi Rob,

    Agree with creating one object and apply specific patterns.
    Have not been able to run the entire test cases for the ParsePosition change; but thanks for sharing, it was new for me.

    Thanks...
    Rob Spoor
    Sheriff

    Joined: Oct 27, 2005
    Posts: 19693
        
      20

    You're welcome.
    Tarun Trehan
    Ranch Hand

    Joined: Mar 16, 2012
    Posts: 49

    Rob/All,

    Thanks for you inputs.
    I was able to accomplish task with above code.
    Listed, entire solution description here.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Date Validation Against Multiple Formats