Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Calendar calendar = Calendar.getInstance()
calendar.setTime(fakeDate);
int currentMonth = calendar.get(Calendar.MONTH);
I get currentMonth == 6 instead of 7.
why is that?
Date fakeDate = sdf.parse("15/07/2013 11:00 AM");
Calendar calendar = Calendar.getInstance()
calendar.setTime(fakeDate);
int currentMonth = calendar.get(Calendar.MONTH);
I get currentMonth == 6 instead of 7.
why is that?
Because Calendar.MONTH
is ZERO based. Why?
Check the docs: (always)
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.
As the doc says - 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.
So try something like this
int currentMonth = calendar.get(Calendar.MONTH)+1;
Because
calendar.get(Calendar.MONTH)
shall give you (currentMonthValue-1) as the value of january starts with 0
It should be
int currentMonth = calendar.get(Calendar.MONTH)+1;