Calendar returns wrong month [duplicate] Calendar returns wrong month [duplicate] java java

Calendar returns wrong month [duplicate]


Months are indexed from 0 not 1 so 10 is November and 11 will be December.


They start from 0 - check the docs


As is clear by the many answers: the month starts with 0.

Here's a tip: you should be using SimpleDateFormat to get the String-representation of the month:

Calendar rightNow = Calendar.getInstance();java.text.SimpleDateFormat df1 = new java.text.SimpleDateFormat("MM");java.text.SimpleDateFormat df2 = new java.text.SimpleDateFormat("MMM");java.text.SimpleDateFormat df3 = new java.text.SimpleDateFormat("MMMM");System.out.println(df1.format(rightNow.getTime()));System.out.println(df2.format(rightNow.getTime()));System.out.println(df3.format(rightNow.getTime()));

Output:

11NovNovember

Note: the output may vary, it is Locale-specific.