Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calendar.getInstance() wrong??

48 views
Skip to first unread message

Eric

unread,
Nov 16, 2004, 5:36:13 PM11/16/04
to
I am using JBuilder 2005 with Tomcat 5 to develop JSPs. Inside my JSP I
have the following code:

int monthA = Calendar.getInstance().MONTH;
int monthB = new Date().getMonth();

monthA is getting the value of 2 (incorrect, it is not March)
monthB is getting the value of 10 (correct, it is November)

My system clock is correct (WinXPsp2)

Any ideas why the Calendar class is reporting the incorrect month?

Thanks in advance.

-Eric


Babu Kalakrishnan

unread,
Nov 16, 2004, 11:13:41 PM11/16/04
to
Eric wrote:
> I am using JBuilder 2005 with Tomcat 5 to develop JSPs. Inside my JSP I
> have the following code:
>
> int monthA = Calendar.getInstance().MONTH;

This is equivalent to

int monthA = Calendar.MONTH;

BK

Todd de Gruyl

unread,
Nov 16, 2004, 11:26:45 PM11/16/04
to
according to the docs:

<http://java.sun.com/j2se/1.5.0/docs/api/java/util/Calendar.html>,

Calendar.MONTH is defined as a static int (You seem to be asking for
something other than what you mean.) You probably want:

Calendar.getInstance().get(Calendar.MONTH)

--
Todd de Gruyl
to...@tdegruyl.com

Murray

unread,
Nov 16, 2004, 11:28:23 PM11/16/04
to

"Babu Kalakrishnan" <k.a...@sankya.com> wrote in message
news:3001v2F...@uni-berlin.de...

To elaborate on this in case you didn't get it, Calendar.MONTH is a static
field that represents the month field number. It isn't the month value. You
need Calendar.getInstance().get(Calendar.MONTH);


Sudsy

unread,
Nov 16, 2004, 11:31:46 PM11/16/04
to

It's not; you're accessing the value of a class variable, not that of an
instance. Your first line is equivalent to the following:

int monthA = Calendar.MONTH;

It the value of the class variable used as a selector in the get method.
Try the following code:

System.out.println( Calendar.MONTH );
System.out.println( Calendar.getInstance().MONTH );
System.out.println( Calendar.getInstance().get( Calendar.MONTH ) );

The first two lines will produce the same result, not matter what month
it is currently. The last shows how to invoke the get method on an
instance, using the aforementioned selector, to get the appropriate value.
HTH

--
Java/J2EE/JSP/Struts/Tiles/C/UNIX consulting and remote development.

Tony Morris

unread,
Nov 17, 2004, 4:09:54 AM11/17/04
to
"Eric" <no...@nowhere.com> wrote in message
news:xhvmd.25300$5b1....@newssvr17.news.prodigy.com...

All the more reason to always use enums where appropriate (manually created
type-safe enums prior to 1.5), not ints, not String, not all the other
nasties I've seen before.

This will prevent mistakes like the one you are making.
I suggest a peek at the API Spec. to figure out why.

--
Tony Morris
http://xdweb.net/~dibblego/

Ann

unread,
Nov 17, 2004, 11:06:16 AM11/17/04
to

"Eric" <no...@nowhere.com> wrote in message
news:xhvmd.25300$5b1....@newssvr17.news.prodigy.com...
> ------------- try this ---------------
Calendar c = Calendar.getInstance();
Y = c.get(Calendar.YEAR);
M = c.get(Calendar.MONTH) + 1;
D = c.get(Calendar.DATE);
System.out.println("Using Today: Y=" + Y + ", M=" + M + ", D=" + D);


0 new messages