Home arrow static arrow Java Programming [Archive] - Date Validation (yyyy/MM/dd) Using Regular Expression
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Date Validation (yyyy/MM/dd) Using Regular Expression
This topic has 2 replies on 1 page.

Posts:28
Registered: 2/7/02
Date Validation (yyyy/MM/dd) Using Regular Expression  
Jul 22, 2004 3:12 AM



 
Hi Friends,

I want to validate date entered by user in yyyy/MM/dd format and for this I want to use Regular Expressions only. Also is there any tool that can be used to generate Regular Expression (for Win2000, Win NT)?

Regards,
Himanshu Rathore
 

Posts:964
Registered: 7/23/03
Re: Date Validation (yyyy/MM/dd) Using Regular Expression  
Jul 22, 2004 3:25 AM (reply 1 of 2)



 
try this
public class Test{	public static void main(String [] args)	{		String regex = "\\d{4}/[01]\\d/[0-3]\\d";		System.out.println("2003/12/11".matches(regex));		System.out.println("2djd/kj3".matches(regex));		System.out.println("22/12/12".matches(regex));		System.out.println("2003/23/05".matches(regex));		System.out.println("1999/12/51".matches(regex));		System.out.println("2007/05/07".matches(regex));	}}

i'm not able to try on it because i only have jdk1.3.1 installed on my computer and these codes
required j2sdk1.4
 

Posts:18,384
Registered: 21.03.00
Re: Date Validation (yyyy/MM/dd) Using Regular Expression  
Jul 22, 2004 3:28 AM (reply 2 of 2)



 
Hi,

Why do you want to use regular expresion for that? You should really use SimpleDateFormat and try to parse the date. The regular expression will not know about how many days each month should have, and if it is a leap year or not.

/Kaj
 
This topic has 2 replies on 1 page.