0

Here is the validation code for Date of format MM/dd/yyyy.

SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
formatter.setLenient(false);
Calendar cal = Calendar.getInstance();
Date myDate = formatter.parse("01/02/2014888");
cal.setTime(myDate);

When the date string is 01/02/2014888, it passes the validation. How can I make the validation correctly?

tim2015
  • 15
  • 4

1 Answers1

1

The Calendar class will allow you to create dates in the future, so 2014888 is a perfectly valid year, albeit a ways off. If you want to add additional constraints to the allowable date, you will need to check the values yourself, such as:

    if (cal.get(Calendar.YEAR) > endOfTime) {
        // do something about it
    }
azurefrog
  • 10,785
  • 7
  • 42
  • 56