[thelist] [Java] Date validation

Chris Blessing webguy at mail.rit.edu
Wed Jan 16 18:26:02 CST 2002


Hi Howard-

I think you've misplaced a method here.  What you're doing is catching
exceptions on the set() method but I think you want to test the method that
actually tries to use the fields you set with set().  Try this:

public static boolean CheckDate(int month, int day, int year){
	java.util.GregorianCalendar gcDate = new java.util.GregorianCalendar();
	gcDate.clear();
	gcDate.setLenient(false);
	gcDate.set(year, month, day);

	try {
		gcDate.get(Calendar.DATE);
	} catch (Exception e) {
		 return false;
	}

	return true;
}

If I'm syntactically incorrect in here anywhere, someone please let me know.
Haven't done Java in a few months...

Chris Blessing
webguy at mail.rit.edu
http://www.330i.net

-----Original Message-----
From: thelist-admin at lists.evolt.org
[mailto:thelist-admin at lists.evolt.org]On Behalf Of Howard Cheng
Sent: Wednesday, January 16, 2002 6:23 PM
To: thelist at lists.evolt.org
Subject: [thelist] [Java] Date validation


Hello all,

I'm trying to do some date validation in Java (JSP) and it's just not doing
what the manual says it should do, so perhaps someone here has an answer.

Here's the code:

public boolean CheckDate(int month, int day, int year)
{   java.util.GregorianCalendar gcDate = new java.util.GregorianCalendar();
     gcDate.clear();
     gcDate.setLenient(false);
     try {
         gcDate.set(year, month, day);
     } catch (Exception e) {
         return false;
     }
     return true;
}

According to the Java(tm) 2 Platform Std Ed v1.3.1 doc...

<excerpt>
When a Calendar is lenient, it accepts a wider range of field values than
it produces. For example, a lenient GregorianCalendar interprets MONTH ==
JANUARY, DAY_OF_MONTH == 32 as February 1. A non-lenient GregorianCalendar
throws an exception when given out-of-range field settings. When calendars
recompute field values for return by get(), they normalize them. For
example, a GregorianCalendar always produces DAY_OF_MONTH values between 1
and the length of the month.
</excerpt>

But my function always returns true. Any ideas?

Thanks.

::::::::::::::::::::::
Howard Cheng
howcheng at ix.netcom.com
AIM: bennyphoebe
ICQ: 47319315


--
For unsubscribe and other options, including
the Tip Harvester and archive of TheList go to:
http://lists.evolt.org Workers of the Web, evolt !





More information about the thelist mailing list