-2

I made a calendar application using the java.util.Calendar class. I am creating a calendar object using cal = Calendar.getInstance(); but for this current month ( November ) it's printing as October.

import java.util.Calendar;

public class Main {
    public static void main (String[] args) {

        Calendar cal = Calendar.getInstance ();

        int y = cal.get (Calendar.YEAR);
        int m = cal.get (Calendar.MONTH);
        int d = cal.get (Calendar.DATE);

        switch(m) {
            case 1:
                System.out.println ("today is " + d + " of January " + y);
                break;
            case 2:
                System.out.println ("today is " + d + " of Febuary " + y);
                break;
            case 3:
                System.out.println("today is " + d + " of March " + y);
                break;
            case 4:
                System.out.println("today is " + d + " of April " + y);
                break;
            case 5:
                System.out.println("today is " + d + " of May " + y);
                break;
            case 6:
                System.out.println("today is " + d + " of June " + y);
                break;
            case 7:
                System.out.println("today is " + d + " of July " + y);
                break;
            case 8:
                System.out.println("today is " + d + " of August " + y);
                break;
            case 9:
                System.out.println("today is " + d + " of September " + y);
                break;
            case 10:
                System.out.println("today is " + d + " of October " + y);
                break;
            case 11:
                System.out.println("today is " + d + " of November " + y);
                break;
            case 12:
                System.out.println("today is " + d + " of December " + y);
                break;
      }

  }
}
Chris Maggiulli
  • 3,375
  • 26
  • 39
  • 7
    Did you read the documentation of `Calendar` and what the `get` method returns? Hint: you will find "Month value is 0-based. e.g., 0 for January." somewhere in there. It's horrible api design that confused many developers before you. – f1sh Nov 24 '21 at 14:08
  • 1
    *1.* Please don't use the obsolete time and date classes from `java.util`. Use the newer classes from `java.time` instead. *2.* Please read the [documentation](https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/Calendar.html#MONTH) which says that months in `Calendar` are 0-indexed. – QBrute Nov 24 '21 at 14:10
  • 1
    @f1sh Programmers get confused because an index is 0 based? I get confused when they are not, such as CSS's `nth-child` or `LocalDateTime#getDayOfMonth()` – Ruan Mendes Nov 24 '21 at 14:41
  • You have discovered one of the many reasons to never use the legacy classes `Calendar`, `Date`, `SimpleDateFormat`, etc. These terrible classes were years ago supplanted by the modern *java.time* classes defined in JSR 310. `LocalDate ld = LocalDate.now();` & `System.out.println( "Today is " + ld.getDayOfMonth() + " of " + ld.getMonth().getDisplayName( TextStyle.FULL , Locale.US ) ) ;` See this code run live at [IdeOne.com](https://ideone.com/jqAd2V). – Basil Bourque Nov 24 '21 at 23:20

2 Answers2

3

Solution

January starts at 0. To avoid this confusion you should use the static constants provided by Calendar.

switch (m) {
    case Calendar.JANUARY:
        System.out.println ("today is " + d + " of January " + y);break;
    case Calendar.FEBRUARY:
        System.out.println ("today is " + d + " of Febuary " + y);break;
    case Calendar.MARCH:
        System.out.println("today is " + d + " of March " + y);break;
    case Calendar.APRIL:
        System.out.println("today is " + d + " of April " + y);break;
    case Calendar.MAY:
        System.out.println("today is " + d + " of May " + y);break;
    case Calendar.JUNE:
        System.out.println("today is " + d + " of June " + y);break;
    case Calendar.JULY:
        System.out.println("today is " + d + " of July " + y);break;
    case Calendar.AUGUST:
        System.out.println("today is " + d + " of August " + y);break;
    case Calendar.SEPTEMBER:
        System.out.println("today is " + d + " of September " + y);break;
    case Calendar.OCTOBER:
        System.out.println("today is " + d + " of October " + y);break;
    case Calendar.NOVEMBER:
        System.out.println("today is " + d + " of November " + y);break;
    case Calendar.DECEMBER:
        System.out.println("today is " + d + " of December " + y);break;
}

The Calendar class is obsolete and instead you should use classes provided by the java.time.* package

Also, you can print the same String without the case statement

Calendar cal = Calendar.getInstance ();
int y = cal.get(Calendar.YEAR);
String m = new SimpleDateFormat("MMMM").format(cal.getTime());
int d = cal.get(Calendar.DATE);

System.out.println(String.format("Today is %d of %s %d", d, m, y));
// Today is 24 of November 2021
Chris Maggiulli
  • 3,375
  • 26
  • 39
2

The unexpected result you are getting is because months are 0-indexed. Which means, January is 0, and so on.

Also I would recommend using the date classes from java.time because it is newer. But feel free to use your own

Chris Maggiulli
  • 3,375
  • 26
  • 39
ihsan
  • 365
  • 3
  • 11