0

All I must do for this code is display the current year, month and day of the year using the GregorianCalendar class. for some reason everything is right but the month. The program returns the month as being 11 instead of 12. Any suggestions?

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    GregorianCalendar Calendar = new GregorianCalendar();
    int year = Calendar.get (GregorianCalendar.YEAR);
    int month = Calendar.get (GregorianCalendar.MONTH);
    int day = Calendar.get (GregorianCalendar.DAY_OF_MONTH);

    System.out.println("the current year is " + year);
    System.out.println("the current month is " + month);
    System.out.println("the current day is " + day);
}
Hoopje
  • 12,677
  • 8
  • 34
  • 50

3 Answers3

3

Java stores the month numbers as 0-based in Calendar. When you use MONTH, 11 does represent December, so this is correct. You must add 1 to this output to convert to what we're used to -- a range of 1-12.

The first month of the year in the Gregorian and Julian calendars is JANUARY which is 0

rgettman
  • 176,041
  • 30
  • 275
  • 357
0

just add +1, java is counting months starting from 0 i.e.

GregorianCalendar.JANUARY == 0
wgitscht
  • 2,676
  • 2
  • 21
  • 25
0

Calendar.MONTH is zero-based. Because of this it is 11.

From the API:

The first month of the year is JANUARY which is 0; the last depends on the number of months in a year.

Semih Eker
  • 2,389
  • 1
  • 20
  • 29