I have a date string “2012-01-26” which I get from JSON.
I have to now check if the month and year is as current year than display data it in ListView.
SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-DD");
try {
Calendar c = Calendar.getInstance();
c.setTime(dateFormat.parse(event_start_date));
int month = c.get(Calendar.MONTH);
System.out.println(c.get(Calendar.YEAR));
System.out.println("month is"+month);
}
Year value is always correct, but the Month is always zero.What am I missing guys?
You are fetching month using Calendar’s instance.
So you will get current month (i.e. January)
And it means 0. So the result is correct.
Check this.
month – The month that was set (0-11) for compatibility with Calendar.
If you want the month of
event_start_date, then you won’t have to use calendar’s instance at all.You can fetch it using