Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Calendar problem

Reply
Thread Tools

Calendar problem

 
 
bort
Guest
Posts: n/a
 
      01-07-2004
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


 
Reply With Quote
 
 
 
 
Wendy S
Guest
Posts: n/a
 
      01-07-2004
"bort" <(E-Mail Removed)> wrote in message
news:Hv%Kb.39599$(E-Mail Removed) .cable.rogers.com...
> 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


 
Reply With Quote
 
 
 
 
bort
Guest
Posts: n/a
 
      01-08-2004
Great!

Thanks Wendy...

bort

"Wendy S" <(E-Mail Removed)> wrote in message
news:bti26t$k2q$(E-Mail Removed)...
> "bort" <(E-Mail Removed)> wrote in message
> news:Hv%Kb.39599$(E-Mail Removed) .cable.rogers.com...
> > 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
>
>



 
Reply With Quote
 
P.Hill
Guest
Posts: n/a
 
      01-08-2004
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

 
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
Calendar GadGets does not show calendar iccsi Windows 64bit 4 08-14-2011 01:52 PM
How to Synchronise Hotmail Calendar with Standard Outlook Calendar using Outlook Connector ?? Synapse Syndrome Computer Support 0 12-02-2007 04:19 AM
Thunderbird Calendar with Exchange 2003 Calendar and Public FoldersCalendar jincmcse Firefox 1 09-03-2005 02:46 AM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net 3 06-23-2004 01:41 PM
Calendar Control - Programatically set the calendar to a date range Shevek ASP .Net Web Controls 0 06-23-2004 12:06 PM



Advertisments