Askar Zaitov
Askar Zaitov

Reputation: 283

Calendar.MONTH returns wrong value

I want to get current month from Calendar.

I used two ways: First:

int gcmonth = Calendar.getInstance().get(Calendar.MONTH);

Second:

Calendar c = (Calendar)Calendar.getInstance();
int gcmonth = c.get(Calendar.MONTH);

Both ways returns 2 (March), but I want to get 8 (September - current month).

EDIT: The date on my phone is right! September 12, 2013 23:32 (Kyiv time)

Upvotes: 2

Views: 1848

Answers (2)

Helmisek
Helmisek

Reputation: 1319

SimpleDateFormat sdfDate = new SimpleDateFormat("MM", Locale.getDefault());
String currentDate = sdfDate.format(new Date());

This is it. It will return 09. September.

Upvotes: 0

Mohit
Mohit

Reputation: 11314

Both of your method totally depends on Device Date

Check your Phone date it will receive date from your device and will give the output

Calendar c = (Calendar)Calendar.getInstance();
int gcmonth = c.get(Calendar.MONTH);

this returns 8

this also returns 8

Calendar.getInstance().get(Calendar.MONTH);

There is no issue with your code only it is due to Device date

Upvotes: 2

Related Questions