Date Validation

Does anyone know if there is a pre-defined date validation service out there somewhere that I can use? I want to take a string which contains a date, for example, in the form of DD-MM-YYYY and determine if it is a valid date.



I’m not aware of an existing service, but it should be pretty straight-forward to create one of your own. Here’s some logic in JavaScript that you can dupe in a Java service:

[url=“Javascript Date Validation”][/url]

Another strategy that I’ve used in the past which provides the flexibility of SimpleDateFormat is to convert the string to a Date using SimpleDateFormat (you’ll need to catch ParseException) and then convert the Date back to a string. Compare the original string with the converted Date string–if they are the same, the date string is valid. If they are not equivalent, then the date string was not valid.

As you may know, SimpleDateFormat.parse() rolls up components that are too big. For example, if the incoming string is 2/29/01, the Date returned will be 3/01/01 because there is no 2/29 and 1 day past 2/28 is 3/1. It does this for all components. Therefore, the convert to date then to string technique catches component errors without you having to (re)implement all the logic for then number of days in month, leap years, leap year exceptions, etc.

Hope this helps.