Calendar problem

Discussion in 'Java' started by bort, Jan 7, 2004.

  1. bort

    bort Guest

    Hi all

    I have two dates entered into two separate Calendar objects(checkin and
    checkout). For "checkin" I set the year to 2004, the month to 1, and the
    date to 31. For "checkout" I set the year to 2004, the month to 2, and the
    date to 1.

    i.e.

    checkin --> January 31, 2004
    checkout --> February 1, 2004

    Now, when I compare the two dates, to ensure that the checkout is AFTER the
    checkin, I get the unexpected result that the checkout date is BEFORE the
    checkin date!

    Does anyone know why? My code snippet is as follows:


    Calendar checkin = Calendar.getInstance();
    checkin.clear();
    checkin.set(2004, 1, 31);

    Calendar checkout = Calendar.getInstance();
    checkout.clear();
    checkout.set(2004, 2, 1);

    if (!checkout.after(checkin)) {
    System.out.println("do something");
    }


    TIA,
    bort
     
    bort, Jan 7, 2004
    #1
    1. Advertising

  2. bort

    Wendy S Guest

    "bort" <> wrote in message
    news:Hv%Kb.39599$...
    > Now, when I compare the two dates, to ensure that the checkout is AFTER

    the
    > checkin, I get the unexpected result that the checkout date is BEFORE the
    > checkin date!


    This is the thing where the month is zero-based. January is 0 not 1.
    Gotcha!

    checkin.set(2004, 1, 31);
    is February 31st, which gets changed to March 2nd

    checkout.set(2004, 2, 1);
    is March 1st

    And yes, March 2nd is after March 1st.

    Add this:
    SimpleDateFormat sdf = new SimpleDateFormat( "yyyy MM dd" );
    System.out.println( "in: "+sdf.format( checkin.getTime() ) );
    System.out.println( "out: "+sdf.format( checkout.getTime() ) );

    And you'll see it:
    in: 2004 03 02
    out: 2004 03 01

    --
    Wendy in Chandler, AZ
     
    Wendy S, Jan 7, 2004
    #2
    1. Advertising

  3. bort

    bort Guest

    Great!

    Thanks Wendy...

    bort

    "Wendy S" <> wrote in message
    news:bti26t$k2q$...
    > "bort" <> wrote in message
    > news:Hv%Kb.39599$...
    > > Now, when I compare the two dates, to ensure that the checkout is AFTER

    > the
    > > checkin, I get the unexpected result that the checkout date is BEFORE

    the
    > > checkin date!

    >
    > This is the thing where the month is zero-based. January is 0 not 1.
    > Gotcha!
    >
    > checkin.set(2004, 1, 31);
    > is February 31st, which gets changed to March 2nd
    >
    > checkout.set(2004, 2, 1);
    > is March 1st
    >
    > And yes, March 2nd is after March 1st.
    >
    > Add this:
    > SimpleDateFormat sdf = new SimpleDateFormat( "yyyy MM dd" );
    > System.out.println( "in: "+sdf.format( checkin.getTime() ) );
    > System.out.println( "out: "+sdf.format( checkout.getTime() ) );
    >
    > And you'll see it:
    > in: 2004 03 02
    > out: 2004 03 01
    >
    > --
    > Wendy in Chandler, AZ
    >
    >
     
    bort, Jan 8, 2004
    #3
  4. bort

    P.Hill Guest

    Here are a few simple suggestions that work for me:

    Wendy S wrote:
    > This is the thing where the month is zero-based. January is 0 not 1.
    > Gotcha!
    >
    > checkin.set(2004, 1, 31);


    To help avoid this problem in the future using Calendar.JANUARY etc.

    > SimpleDateFormat sdf = new SimpleDateFormat( "yyyy MM dd" );


    when debugging more is better so use MMMM and you'll see the word (even
    if that is not what you are using in your program, when displaying it
    it is useful).

    > SimpleDateFormat sdf = new SimpleDateFormat( "yyyy MM dd" );
    > System.out.println( "in: "+sdf.format( checkin.getTime() ) );


    By the way:
    System.out.println( "in : " + checkin.getTime() );
    would give even more with less typing.

    HTH,
    -Paul
     
    P.Hill, Jan 8, 2004
    #4
    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. Michael England
    Replies:
    0
    Views:
    621
    Michael England
    Nov 26, 2003
  2. Alessandro

    Problem with object calendar

    Alessandro, Jan 13, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    292
    Alessandro
    Jan 13, 2004
  3. Shevek
    Replies:
    3
    Views:
    6,122
    Shevek
    Jun 23, 2004
  4. Shevek
    Replies:
    0
    Views:
    424
    Shevek
    Jun 23, 2004
  5. Parthiv Joshi
    Replies:
    1
    Views:
    806
    Samuel L Matzen
    Jul 6, 2004
Loading...

Share This Page