1

I have following code which checks for the valid format date

private void validateDate(String date){

    try {
        String validDate = "MM/dd/yyyy";    
        SimpleDateFormat format = new SimpleDateFormat(validDate);
        format.setLenient(false);
        Date theDate = new Date();
        theDate = format.parse(date);
    }
    catch (Exception e) {
    }

}  

I am passing the date value as 06/25/20014. In this year format is wrong and I was expecting that it will throw exception and will go inside catch, but it never happens and it successfully passes the code format.parse(date); due to which my application is not throwing error.

I also debugged the line format.parse(date); and it returns "Fri Jul 31 00:00:00 MST 20015". I am not sure why that line is not throwing any exception.

Thanks for your help!!

user2810293
  • 135
  • 2
  • 2
  • 11

4 Answers4

0

This is actually documented behaviour (at least for the Gregorian calendar, which I assume you will be using unless you explicitely set it to a different calendar):

Year: If the formatter's Calendar is the Gregorian calendar, the following rules are applied.

For formatting, if the number of pattern letters is 2, the year is truncated to 2 digits; otherwise it is interpreted as a number. For parsing, if the number of pattern letters is more than 2, the year is interpreted literally, regardless of the number of digits.

And yes, 20015 will probably be a valid year some day ;) If you want to check for exactly 4 digits, you might want to consider using a regular expression matching before parsing.

Marvin
  • 13,325
  • 3
  • 51
  • 57
0

If this line did throw an exception, then your program would stop working after year 9999, which is not usually what you want. Sure, you do not expect your program to last this long, but people did not expect to last up to y2k either; so Java's choice not to block at any date seems reasonable.

If you want to check the year is between 1 and 9999, you can just write a if: (see related question I want to get Year, Month, Day, etc from Java Date to compare with Gregorian Calendar date in Java. Is this possible?)

Calendar cal = Calendar.getInstance();
cal.setTime(date);
if (cal.get(Calendar.YEAR) > 9999)
    throw new RuntimeException("Blah");
Community
  • 1
  • 1
Ekleog
  • 1,054
  • 7
  • 19
0

In the standard date formats for SimpleDateFormat, the number 'y' 's doesn't necessarily correspond to the number of digits (it isn't a regex). One or two y's is an indicator for a 2 digit year (15, 98, etc.) and 3 or more y's is an indicator for the full year.

Brendan
  • 136
  • 1
  • 10
0

I can see that the provided date is valid (although 18k years ahead)

public static void validateDate(String date){

    try {
        String validDate = "MM/dd/yyyy";    
        SimpleDateFormat format = new SimpleDateFormat(validDate);
        format.setLenient(false);
        Date theDate = format.parse(date);
        Calendar c = new GregorianCalendar();
        c.setTime(theDate);
        int year = c.get(Calendar.YEAR);
        int month  = c.get(Calendar.MONTH);
        int day  = c.get(Calendar.DAY_OF_MONTH);
        int pos = c.get(Calendar.DAY_OF_WEEK);
        if (Calendar.WEDNESDAY == pos) 
          System.out.print("day is Wednesday on " + year + "/" + (month+1) + "/" + day);
    }
    catch (Exception e) {
       System.out.print("Inside exception block " + e);
    }
}

prints: day is Wednesday on 20014/6/25

If you need some year validation you can add additional check

if (year > MAX_YEAR) {
  // add error here
}

Here is DEMO

MaxZoom
  • 7,619
  • 5
  • 28
  • 44