tim2015
tim2015

Reputation: 15

Java Date Validation Issue

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?

Upvotes: 0

Views: 131

Answers (1)

azurefrog
azurefrog

Reputation: 10945

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
    }

Upvotes: 1

Related Questions