2

I am trying to check a date for validation. Below is my code. When I check for the date 12/12/2014XYZ, SimpleDateFormat.parse does not throw exception. With other invalid dates like 13/13/2014 it is throwing exception.

public class DateCheck{

    public static void main(String[] args) {
        SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
        sdf.setLenient(false);
        try {
            sdf.parse("12/31/2014XYZ");
            System.out.println("Valid Date");
        } catch (Exception e) {
            System.out.println("Invalid Date");

        }
    }
}

I googled a lot but could not find the proper solution.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Ajay Sainy
  • 279
  • 1
  • 9
  • 21

4 Answers4

3

Use a ParsePosition as follows:

try {
    String input = "12/31/2014XYZ";

    ParsePosition pp = new ParsePosition(0);
    sdf.parse(input, pp);

    // Make sure the entire string was parsed
    if (pp.getIndex() != input.length())
        throw new Exception();

    System.out.println("Valid Date");
} catch (Exception e) {
    System.out.println("Invalid Date");
}

Similar questions:

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
0

This is as described in the Javadoc:

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

So your observation is the behaviour that is expected and described.

Uwe Plonus
  • 9,803
  • 4
  • 41
  • 48
0

As you have told the date format "MM/dd/yyyy" so it will check only first four characters after "/"

if you write the date as "12/31/XYZ2014" it will throw an exception and invalid date will be printed.

faisalbhagat
  • 2,142
  • 24
  • 27
0

From Java doc.

Parses text from the beginning of the given string to produce a date. The method may not use the entire text of the given string.

So it will take 12/31/2014 only from 12/31/2014XYZ and take as valid format.

Eg:

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
try {
    java.util.Date date=sdf.parse("12/31/2014AnyThing");
    System.out.println("Valid Date "+date);
   } catch (ParseException e) {
    System.out.println("Invalid Date");
   }
try {
    java.util.Date date=sdf.parse("AnyThing12/31/2014");
    System.out.println("Valid Date "+date);
   } catch (ParseException e) {
    System.out.println("Invalid Date");
  }

Out put:

  Valid Date Wed Dec 31 00:00:00 IST 2014
  Invalid Date

Same as here.

   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
    try {
        java.util.Date date=sdf.parse("12/31/2014XYZ");
        System.out.println("Valid Date "+date);
    } catch (ParseException e) {
        System.out.println("Invalid Date");

    }

    try {
        java.util.Date date=sdf.parse("XYZ12/31/2014");
        System.out.println("Valid Date "+date);
    } catch (ParseException e) {
        System.out.println("Invalid Date");

    }

Out put:

   Valid Date Wed Dec 31 00:00:00 IST 2014
   Invalid Date

Edit:

@aioobe answer will provide the solution for not take this as valid date format.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115