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

Discussion in 'Java' started by lbrtchx@hotmail.com, Dec 22, 2006.

  1. Guest

    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
     
    , Dec 22, 2006
    #1
    1. Advertising

  2. In article <>,
    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
     
    Steve W. Jackson, Dec 22, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QnJhZCBSb2JlcnRz?=
    Replies:
    1
    Views:
    384
    =?Utf-8?B?UGF1bA==?=
    Aug 23, 2004
  2. =?Utf-8?B?am9l?=

    day month year

    =?Utf-8?B?am9l?=, Apr 8, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    403
    Teemu Keiski
    Apr 8, 2006
  3. Laguna
    Replies:
    20
    Views:
    798
    John Machin
    Sep 15, 2005
  4. Highlander
    Replies:
    13
    Views:
    2,887
    Highlander
    Jan 2, 2007
  5. Rob Redmon

    Convert day of year to month, day

    Rob Redmon, Apr 4, 2008, in forum: Ruby
    Replies:
    7
    Views:
    310
    Alex Wayne
    Apr 4, 2008
Loading...

Share This Page