Simple Date Validation

Author:   Senthil Kumar
Website: http://kolkataprince.blogspot.com

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 Books
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.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.