How do you use SimpleDateFormat to PREVENT a user from entering a 5 digit year ?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Christopher_Lupton
Posted On:   Tuesday, July 29, 2003 12:29 PM

How do you prevent a user from entering a 5 digit year, for example, "07/11/20033". The code below will not throw an exception and ,at least at first glance, does not seem to provide anything immediately useful to indicate that "20033" should not be allowed as a valid date. [obviously the import statements are left out] public static void main(String []args) { try { String format = "yyyy-MM-dd"; DateFormat df = new SimpleDateFormat( format ); df.setLenient(false); Date convertedDate = df.parse("20033-07-11"); if(convertedDate != null) {    More>>

How do you prevent a user from entering a 5 digit year, for example, "07/11/20033".


The code below will not throw an exception and ,at least at first glance, does not seem to provide anything immediately useful to indicate that "20033" should not be allowed as a valid date.


[obviously the import statements are left out]



public static void main(String []args)

{



try

{

String format = "yyyy-MM-dd";

DateFormat df = new SimpleDateFormat( format );

df.setLenient(false);

Date convertedDate = df.parse("20033-07-11");

if(convertedDate != null)

{

System.out.println("The Test Converted Date is : " + convertedDate);

}

else

{

System.out.println("The Test Converted Date is NULL !!");

}

}

catch(Exception ex)

{

System.out.println("Date is not in valid format of MM/dd/yyyy .");

}

}

   <<Less

Re: How do you use SimpleDateFormat to PREVENT a user from entering a 5 digit year ?

Posted By:   David_Bates  
Posted On:   Wednesday, July 30, 2003 12:23 AM

It might be possible, but I don't think that's really the job SimpleDateFormat - it's job is formatting, not validating.



I would suggest that using Regular Expressions might be a better idea:


^d{4}-d{2}-d{2}$


(I knocked this up very quickly - it doesn't prevent you entering 2003-16-50, but you get the point.

Re: How do you use SimpleDateFormat to PREVENT a user from entering a 5 digit year ?

Posted By:   Christopher_Lupton  
Posted On:   Tuesday, July 29, 2003 12:31 PM

Ok, even after reviewing my post 5 times before submitting it.. I still had a typo in the last println statement. it should have been...



System.out.println("Date is not in valid format of yyyy/MM/dd .");

About | Sitemap | Contact