I\'m using
java.util.Date date;
SimpleDateFormat format = new SimpleDateFormat(\"dd/MM/yyyy\");
try {
date = sdf.parse(inputString);
} catch (ParseExceptio
Set the Leniency bit:
public void setLenient(boolean lenient)
Specify whether or not date/time parsing is to be lenient. With lenient parsing, the parser may use heuristics to interpret inputs that do not precisely match this object's format. With strict parsing, inputs must match this object's format.
The following code:
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class Tester {
public static void main(String[] argv) {
java.util.Date date;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
// Lenient
try {
date = sdf.parse("40/02/2013");
System.out.println("Lenient date is : "+date);
} catch (ParseException e) {
e.printStackTrace();
}
// Rigorous
sdf.setLenient(false);
try {
date = sdf.parse("40/02/2013");
System.out.println("Rigorous date (won't be printed!): "+date);
} catch (ParseException e) {
e.printStackTrace();
}
}
}
Gives:
Lenient date is : Tue Mar 12 00:00:00 IST 2013
java.text.ParseException: Unparseable date: "40/02/2013"
at java.text.DateFormat.parse(DateFormat.java:357)