1

I want to get date in the format of MMyyyy and here is my code

      SimpleDateFormat sdf = new SimpleDateFormat("MMyyyy");
      String d="02-2014";
      Date d1=sdf.parse(d);
      System.out.println("the date is:" +d1);

As my string is not in a given format it should throw an exception but it is giving an output like

the date is:Sat Jun 01 00:00:00 IST 2272

Help me out

2
  • 1
    Call sdf.setLenient(false) before using it . Commented Jun 22, 2017 at 9:39
  • Its Working Thanks a lot Commented Jun 22, 2017 at 9:42

1 Answer 1

3

add sdf.setLenient(false); and you get an exception

SimpleDateFormat sdf = new SimpleDateFormat("MMyyyy");
sdf.setLenient(false);
String d="02-2014";
Date d1=sdf.parse(d);
System.out.println("the date is:" +d1);

For more informations read the javadoc

Sign up to request clarification or add additional context in comments.

1 Comment

Its Working Thanks a lot

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.