Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

1.4 Problem with Calendar.MONTH - returns previous month

Status
Not open for further replies.

baden

Programmer
Feb 6, 2002
125
US
The following code:

Calendar rightNow = Calendar.getInstance();
System.out.println("\n\nMONTH: " + rightNow.get(Calendar.MONTH));

returns 5 instead of 6 for June. Anyone else have this problem??? (JDK v1.4.0)
 
Correct me if I'm wrong but doesn't Java count with 0 being the first value for the months. January =0, Feb=1, March =2,....Dec=11.

It's just like arrays.

-a5k
 
This is not a bug. It is the intended functionality of Calendar. It clearly states this in the Javadocs.

From Javadocs for Calendar:
Code:
MONTH

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.
 
Forget it... Months are zero-based.

Solution:

System.out.println("\n\nMONTH: " + (rightNow.get(Calendar.MONTH)+1));
 
Man, you guys are fast :) Thanks for the replies!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top