• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Date Validation Against Multiple Formats

 
Tarun Trehan
Ranch Hand
Posts: 51
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??


     
    Paul Clapham
    Sheriff
    Posts: 21322
    32
    Eclipse IDE Firefox Browser MySQL Database
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20611
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 20611
    63
    Chrome Eclipse IDE Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're welcome.
     
    Tarun Trehan
    Ranch Hand
    Posts: 51
    Android Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Rob/All,

    Thanks for you inputs.
    I was able to accomplish task with above code.
    Listed, entire solution description here.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic