help with Calendar??

R

roy

Hi,

I got incorrect date using the following code.

Calendar rightnow = Calendar.getInstance();
Date d = new Date();
System.out.println(d.toString());
rightnow.setTime(d);
System.out.println(rightnow.get(Calendar.YEAR));
System.out.println(rightnow.get(Calendar.MONTH));
System.out.println(rightnow.get(Calendar.DAY_OF_MONTH));

The output is:
Tue Oct 14 19:46:53 EDT 2003
2003
9
14

Does anyone know why rightnow.get(Calendar.MONTH) gives me 9, but not 10?
Thanks a lot
 
C

Christophe Vanfleteren

roy said:
Hi,

I got incorrect date using the following code.

Calendar rightnow = Calendar.getInstance();
Date d = new Date();
System.out.println(d.toString());
rightnow.setTime(d);
System.out.println(rightnow.get(Calendar.YEAR));
System.out.println(rightnow.get(Calendar.MONTH));
System.out.println(rightnow.get(Calendar.DAY_OF_MONTH));

The output is:
Tue Oct 14 19:46:53 EDT 2003
2003
9
14

Does anyone know why rightnow.get(Calendar.MONTH) gives me 9, but not 10?
Thanks a lot

Calendar starts counting the months at index 0.
So January = month 0, February = month 1, ...
 
S

Sudsy

roy said:
Hi,

I got incorrect date using the following code.

Calendar rightnow = Calendar.getInstance();
Date d = new Date();
System.out.println(d.toString());
rightnow.setTime(d);
System.out.println(rightnow.get(Calendar.YEAR));
System.out.println(rightnow.get(Calendar.MONTH));
System.out.println(rightnow.get(Calendar.DAY_OF_MONTH));

The output is:
Tue Oct 14 19:46:53 EDT 2003
2003
9
14

Does anyone know why rightnow.get(Calendar.MONTH) gives me 9, but not 10?
Thanks a lot

Because in the javadocs it says so? Here's an extract from
the java.util.Calendar page:
public static final int MONTH

Field number for get and set indicating the month. This is a
calendar-specific value. The first month of the year is JANUARY which
is 0; the last depends on the number of months in a year.

Origin = 0, just like hour of day. Right there in the documentation...
 

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

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top