How to Validate a Date in Java

Save

When receiving input in Java, you'll want to validate it before passing it to other parts of your program to avoid bugs. Validating the user's input before accepting it into the program allows you to bounce faulty input back to the user. While the variety of choices in date expression can make date input and validation troublesome, a simple method allows you to check your input and ensure properly formatted dates.

Things You'll Need

  • Text editor and compiler or Java IDE
  • Decide on the date format you will require users to use for program input. For example, you might select month/date/year.

  • Create a Boolean method that accepts the input date in the form of a string:

    private isValidDate(String input){ }

  • Convert your ideal date format into a string that Java can understand. Substitute capital "M" for each character of month, lowercase "d" for each character of day in month, and lowercase "y" for each character of year. For example if you expect your users to input Dec. 31, 1999, as 12/31/1999, your date format string would be MM/dd/yyyy. Save this string as formatString in your isValidDate method.

    String formatString = "MM/dd/yyyy";

  • Import the required classes into the class containing your isValidDate method.

    import java.text.*;

  • Open a try block in your isValidDate method. Within this block create a new SimpleDateFormat called format with formatString as the input to its constructor.

    try{

    SimpleDateFormat format = new SimpleDateFormat(form);
  • Call format.setLenient(false) to ensure that the input string will be properly parsed. Now pass the input string into format.parse() and close the try block. The SimpleDateFormat will attempt to match the input string to your provided formatString. If the input string contains an improperly formatted date, SimpleDateFormat will throw an exception.

    format.setLenient(false);
    format.parse(input);
    }

  • Outside of the try block, catch ParseException and IllegalArguementException. Return false if either of these exceptions has occurred.

    catch (ParseException e){return false;}
    catch (IllegalArguementException e){return false;}

  • If your program has reached this point, the input date was valid and you can return true and close your method.

    return true;

  • Integrate the method into your program. Call isValidDate(input) on any input string. The method will return true if the input string matches the date format you specified in Step 3.

Tips & Warnings

  • Your final method will resemble the following:
  • boolean isValidDate(String input){
  • String formatString = "MM/dd/yyyy"
  • try{
  • SimpleDateFormat format = new SimpleDateFormat(form);
  • format.setLenient(false);
  • format.parse(input);
  • }
  • catch (ParseException e){return false;}
  • catch (IllegalArguementException e){return false;}
  • return true;

References

  • Photo Credit calendar 2007 image by Franc Podgor...¡ek from Fotolia.com
Promoted By Zergnet

Comments

Resources

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!