Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > verifying a date is valid based on integer values for year, month and day of month

Reply
Thread Tools

verifying a date is valid based on integer values for year, month and day of month

 
 
lbrtchx@hotmail.com
Guest
Posts: n/a
 
      12-22-2006
Say you have a Calendar instance.
..
Calendar Klndr = java.util.Calendar.getInstance();
Klndr.setLenient(false);
Klndr.clear();
Klndr.set(Calendar.YEAR, iYOD);
Klndr.set(Calendar.MONTH, iIxMonth);
Klndr.set(Calendar.DAY_OF_MONTH, iDOD);
..
How could you see if the date was write?
..
I don't see any exceptions being thrown in the class to complain for
invalid dates
..
lbrtchx

 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      12-22-2006
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

> Say you have a Calendar instance.
> .
> Calendar Klndr = java.util.Calendar.getInstance();
> Klndr.setLenient(false);
> Klndr.clear();
> Klndr.set(Calendar.YEAR, iYOD);
> Klndr.set(Calendar.MONTH, iIxMonth);
> Klndr.set(Calendar.DAY_OF_MONTH, iDOD);
> .
> How could you see if the date was write?
> .
> I don't see any exceptions being thrown in the class to complain for
> invalid dates
> .
> lbrtchx


If you read the discussion in the Javadocs on Calendar, you might want
to reconsider three separate calls to set() because of potential
interactions between fields. Instead, I suggest using the alternate
method set(year, month, date) to set all three at once. Once that's
done, I suspect that the easiest way to validate your three values is to
then compare each to what's in the Calendar.

Of course, that overlooks your call to setLenient(false) above. The API
says you should get an exception if any of those values is out of bounds
when calling set() for individual fields. But my reading suggests that
doesn't cover all your bases (and I haven't tested it). I think it
means you can't pass 15 for a month, for instance. But what happens
when you pass 30 for a day when you've previously set the month to
February isn't obvious. That's why I think you might be better off
letting it stay lenient, using the set() version that takes all three
values, and then comparing the result against the ones you sent in.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Now.day and day(now) returning the wrong day! Andy ASP .Net 1 11-06-2008 12:20 PM
Cal control - pre selecting a date from last month - and the calendar still shows this month. Mufasa ASP .Net 0 09-05-2008 06:51 PM
Convert day of year to month, day Rob Redmon Ruby 7 04-04-2008 04:32 PM
verifying valid Pointer andreas C++ 16 01-26-2005 11:35 AM
Verifying valid input in a loop JR C++ 7 09-13-2004 08:57 PM



Advertisments