Checking if date parsing is correct

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-03-30T10:46:37Z Indexed on 2010/03/30 10:53 UTC
Read the original article Hit count: 437

Filed under:
|
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about date