Calendar.getInstance() wrong??

E

Eric

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
 
B

Babu Kalakrishnan

Eric said:
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
 
T

Todd de Gruyl

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?
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)
 
M

Murray

Babu Kalakrishnan said:
This is equivalent to

int monthA = Calendar.MONTH;

BK

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);
 
S

Sudsy

Eric said:
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?

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
 
T

Tony Morris

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

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.
 
A

Ann

Eric said:
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
------------- 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);
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top