| Simple Date
Validation
Author: Senthil Kumar
import java.util.Date;
import java.text.ParseException;
import java.text.DateFormat;
public class validateDate
{
public static void main(String args[])
{
String dt = "04/31/2005"; //Invalid Date
//String dt = "02/29/2004"; //Valid Date
try
{
DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
df.setLenient(false); // this is important!
Date dt2 = df.parse(dt);
System.out.println("Date is ok = " + dt2);
}
catch (ParseException e)
{
System.out.println("Invalid date " + dt);
}
catch (IllegalArgumentException e)
{
System.out.println("Invalid date " + dt);
}
}
}//End of validateDate
Related:
Java Certification, Programming, JavaBean and Object Oriented Reference Books Return to : Java Programming Hints and Tips All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
|