Checking if date parsing is correct
- by Javi
Hello,
I have this code for checking whether the Date is OK or not, but it's not ckecking all the cases. For example when text="03/13/2009" as this date doesn't exist in the format "dd/MM/yyyy" it parses the date as 03/01/2010. Is there any way to change this behaviour and getting an exception when I try to parse a Date which is not correct? What's the best way to do this validation?
public static final String DATE_PATTERN = "dd/MM/yyyy";
public static boolean isDate(String text){
SimpleDateFormat formatter = new SimpleDateFormat(DATE_PATTERN);
ParsePosition position = new ParsePosition(0);
formatter.parse(text, position);
if(position.getIndex() != text.length()){
return false;
}else{
return true;
}
}
Thanks.