Home » Uncategorized » Date validation with java

Date validation with java

Kevin on Twitter

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Hi ya'll JDevs..I have a code snippet below that would help you solve problems in date validation.
Its a method that takes a string parameter as date and uses the SimpleDateFormat library to validate the date
// date validation using SimpleDateFormat
// it will take a string and make sure it's in the proper
// format as defined by you, and it will also make sure that
// it's a legal date

public boolean isValidDate(String date)
    // set date format, this can be changed to whatever format
    // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
    // you can read more about it here:
    // http://java.sun.com/j2se/1.4.2/docs/api/index.html

    SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");

    // declare and initialize testDate variable, this is what will hold
    // our converted string

    Date testDate = null;

    // we will now try to parse the string into date form
      testDate = sdf.parse(date);

    // if the format of the string provided doesn't match the format we
    // declared in SimpleDateFormat() we will get an exception

    catch (ParseException e)
      errorMessage = "the date you provided is in an invalid date" +
                              " format.";
      return false;

    // dateformat.parse will accept any date as long as it's in the format
    // you defined, it simply rolls dates over, for example, december 32
    // becomes jan 1 and december 0 becomes november 30
    // This statement will make sure that once the string
    // has been checked for proper formatting that the date is still the
    // date that was entered, if it's not, we assume that the date is invalid

    if (!sdf.format(testDate).equals(date))
      errorMessage = "The date that you provided is invalid.";
      return false;

    // if we make it to here without getting an error it is assumed that
    // the date was a valid one and that it's in the proper format

    return true;

} // end isValidDate


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: