-2

Possible Duplicate:
Calendar returns wrong month

I want to retrieve the date and time for my application, for which I wrote the following code

Calendar c = Calendar.getInstance();
System.err.println("Date is: " + c.get(Calendar.DATE));
System.err.println("Month is: " + c.get(Calendar.MONTH));
System.err.println("Year is: " + c.get(Calendar.YEAR));
System.err.println("Hour is: " + c.get(Calendar.HOUR_OF_DAY));

However the preceding code snippet is providing incorrect result.

SEVERE: Date is: 31
SEVERE: Month is: 11
SEVERE: Year is: 2012
SEVERE: Hour is: 17 

NOTE: The time on my machine is perfect, no problem there

Community
  • 1
  • 1
android_newbie
  • 667
  • 2
  • 14
  • 32
  • 6
    What were you expecting? (http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Calendar.html#MONTH) – Oliver Charlesworth Dec 31 '12 at 11:39
  • lol, brilliant. joda-time is good http://joda-time.sourceforge.net/userguide.html – NimChimpsky Dec 31 '12 at 11:40
  • What you got is absolutely right. See the docs: http://docs.oracle.com/javase/7/docs/api/java/util/Date.html – nkr Dec 31 '12 at 11:40
  • Ok..everyone.. ITS MY BAD. PLEASE STOP DOWN VOTING. I WONT BE ABLE TO PUT QUESTION IN FUTURE IF I GET TOO MUCH DOWN VOTES. – android_newbie Dec 31 '12 at 11:43
  • 1
    Tip: Do a *little* more research in the future, which prevents people from downvoting your questions. The answer to your question could have been looked up in the Java documentation. – slhck Dec 31 '12 at 11:44
  • 1
    @user1822826 I think the downvotes are also because you don't explain what is wrong with the output (i.e. you thought the month part was wrong). And downvotes won't prevent you from asking questions. – assylias Dec 31 '12 at 11:46
  • FYI, the troublesome `Calendar` class is now legacy, **supplanted by the java.time classes**. Fortunately the java.time classes use sane numbering, with months 1-12 for January-December. See [Tutorial](https://docs.oracle.com/javase/tutorial/datetime/TOC.html). – Basil Bourque Nov 14 '16 at 03:19

3 Answers3

0

Month is zero indexed. So, 11 means its December.

Azodious
  • 13,752
  • 1
  • 36
  • 71
0

The output that you are getting is correct.

I think you are confused why you are getting month as 11 instead of 12. Right? If that is the question then don't bother. Months are 0 based and hence 0 is January, 1 is Feb and so on...

So output as 11 means is December.

Read Docs

If you want proper month with wording as month, use below.

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()));

it will give

12
Dec
December

Demo

Note: In demo you will see Jan 2013 as the server of this site is somewhere where 2013 already begun. World Time

Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

You are expecting 12 instead of 11 for the month.

c.get(Calendar.MONTH) returns 0 based index.

From the javadoc :

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 in the Gregorian and Julian calendars is JANUARY which is 0; the last depends on the number of months in a year.

Abubakkar
  • 15,488
  • 8
  • 55
  • 83