Validating date in java

27-Sep-2016 09:37

Recently I was looking for a programmatic way for validating data against well known validation e.g. I have to use them inside custom validator and sometimes in controller/action.

Creating Command objects for validating was not suitable in my case(overkilling solution).

Feb.2011 = Sat Feb 19 CET 2011 4/20/2012 = Fri Apr 20 CEST 2012 some string = null 123456 = null null = null Thus you can quickly check weather given string is a valid Date or not using Date Util class.

Generic Url(" //invalid URL assert Generic Credit Card("4111111111111111") // valid visa card number assert !

Generic Credit Card("4111111111111112") // invalid visa card number assert Generic Date("2011-12-30", "yyyy-MM-dd", true) // valid date(last argument for checking strict) assert Generic Date("2011-02-28", "yyyy-MM-dd", true) // valid date - 28th Feb assert !

Writing/looking for regular expression was another solution.

I found a special class import org.apache.commons.validator.

Write code to validate the given date against the given format.