2
Calendar c = Calendar.getInstance();

System.out.println("Mili->>" + c.getTimeInMillis());

System.out.println("Month  ->>" + Calendar.MONTH);

Though I am getting correct time in millisec format, Month is showing as 2 (March??) Why so?

Below is output

Mili->>1434029840778

Month ->>2

Community
  • 1
  • 1
Pallab
  • 547
  • 1
  • 5
  • 13

3 Answers3

4

What you want is the following idiom: c.get(Calendar.MONTH).

Calendar.MONTH per se is just an internal constant and will (hopefully) always return 2.

Example

// We're in June at the time of writing
// Months are 0-based so June == 5
Calendar c = Calendar.getInstance();
System.out.println(Calendar.MONTH);
System.out.println(c.get(Calendar.MONTH));

Output

2
5

See also: API

Mena
  • 46,817
  • 11
  • 84
  • 103
  • i am getting the compile time error "The method get(int) is undefined for the type String" when i am using c.get(Calendar.MONTH) – Pallab Jun 11 '15 at 13:47
  • 1
    You need to use `System.out.println("Month ->>" + c.get(Calendar.MONTH));` , **not** `System.out.println(c.get("Month ->>" + Calendar.MONTH));`! – Mena Jun 11 '15 at 13:48
0

You are displaying the value of the Calendar.MONTH constant, hence 2.

Benjamin
  • 1,786
  • 12
  • 21
0

You're getting calendar c from an instance but calendar from month with the Calendar object, get it from the instance too.

Also - Calendar starts from 0 as referenced here

In other news, use the new DateTime API in Java 8 or JODA time, they're much nicer.

Community
  • 1
  • 1
David
  • 18,969
  • 28
  • 104
  • 128