Exforsys

Online Training

Date Validation using SimpleDateFormat()

This is a discussion on Date Validation using SimpleDateFormat() within the Java Tutorials forums, part of the Articles and Tutorials category; Code: // date validation using SimpleDateFormat // it will take a string and make sure it's in the proper // format as ...


Go Back   Exforsys > Articles and Tutorials > Java Tutorials

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-05-2006, 01:20 PM
Member
 
Join Date: Apr 2006
Posts: 30
insane is on a distinguished road
Date Validation using SimpleDateFormat()

Code:
// date validation using SimpleDateFormat
// it will take a string and make sure it's in the proper 
// format as defined by you, and it will also make sure that
// it's a legal date

public boolean isValidDate(String date)
{
   // set date format, this can be changed to whatever format
   // you want, MM-dd-yyyy, MM.dd.yyyy, dd.MM.yyyy etc.
   // you can read more about it here:
   // http://java.sun.com/j2se/1.4.2/docs/api/index.html
   
   SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
   
   // declare and initialize testDate variable, this is what will hold
   // our converted string
   
   Date testDate = null;

   // we will now try to parse the string into date form
   try
   {
     testDate = sdf.parse(date);
   }

   // if the format of the string provided doesn't match the format we 
   // declared in SimpleDateFormat() we will get an exception

   catch (ParseException e)
   {
     errorMessage = "the date you provided is in an invalid date" +
                             " format.";
     return false;
   }

   // dateformat.parse will accept any date as long as it's in the format
   // you defined, it simply rolls dates over, for example, december 32 
   // becomes jan 1 and december 0 becomes november 30
   // This statement will make sure that once the string 
   // has been checked for proper formatting that the date is still the 
   // date that was entered, if it's not, we assume that the date is invalid

   if (!sdf.format(testDate).equals(date)) 
   {
     errorMessage = "The date that you provided is invalid.";
     return false;
   }
   
   // if we make it to here without getting an error it is assumed that
   // the date was a valid one and that it's in the proper format

   return true;

} // end isValidDate
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Windows Vista Release date kalareddy Windows Vista Tutorials 3 04-11-2007 06:24 AM
Hurry Satyam Off Campus, Last Date for Submition 6th Feb 06. lokeshm Freshers Jobs 1 02-06-2006 02:32 PM
DELL, Last Date : 4th Aug, Freshers 2004-2005, BE, Min 70%. lokeshm Freshers Jobs 0 07-28-2005 11:43 AM
Applabs 2004/2005 last date june 15 kalareddy Freshers Jobs 0 06-13-2005 05:29 AM
OPT extention from the date of expiry till oct 1 laxma Immigration Help 1 09-26-2004 09:46 AM


All times are GMT -4. The time now is 07:07 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.