private static boolean isValidDate ( String date , Calendar out , String separator ) { try { String [ ] dates = date . split ( separator ) ; out . set ( Calendar . DATE , Integer . parseInt ( dates [ dates . length - 1 ] ) ) ; out . set ( Calendar . MONTH , Integer . parseInt ( dates [ dates . length - 2 ] ) ) ; if ( dates . length > 2 ) { out . set ( Calendar . YEAR , Integer . parseInt ( dates [ dates . length - 3 ] ) ) ; } } catch ( Exception e ) { try { out . set ( Calendar . DATE , Integer . parseInt ( date . substring ( date . length ( ) - 2 , date . length ( ) ) ) ) ; out . set ( Calendar . MONTH , Integer . parseInt ( date . substring ( date . length ( ) - 4 , date . length ( ) - 2 ) ) - 1 ) ; if ( date . length ( ) > 4 ) { out . set ( Calendar . YEAR , Integer . parseInt ( date . substring ( date . length ( ) - 8 , date . length ( ) - 4 ) ) - 1 ) ; } } catch ( Exception e2 ) { return false ; } } return true ; }
To check if the input date string is valid and match with the required format