Sairam Sattar
Sairam Sattar

Reputation: 35

Issue in simpledateformat in java

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

Upvotes: 1

Views: 73

Answers (1)

Jens
Jens

Reputation: 69480

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

Upvotes: 3

Related Questions