Calendar problem

B

bort

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
 
W

Wendy S

bort said:
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
 
B

bort

Great!

Thanks Wendy...

bort

Wendy S said:
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
 
P

P.Hill

Here are a few simple suggestions that work for me:

Wendy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top