Sep 30

Use the parse() method of the SimpleDateFormat class, it will throw an exception if the string does not contain a valid date. Use setLenient(false) to force strict parsing.


DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");
dateFormat.setLenient(false);
try
{
   Date d = dateFormat.parse("01/05/06");

   // string contains valid date
}
catch (ParseException ex)
{
   // string contains invalid date
}

try
{
   Date d = dateFormat.parse("99/88/06");

   // string contains valid date
}
catch (ParseException ex)
{
   // string contains invalid date
} 

written by objects \\ tags: ,


Leave a Reply

You must be logged in to post a comment.