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
This is equivalent to
int monthA = Calendar.MONTH;
BK
<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
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);
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.
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/