Calendar issue in Adding month +1 to to calendar month in Android

android, date, java

Solution

you can see the +1 to set field is adding 30 days date different to your dates(observed from your output.)

if you want months then use the code

Calendar cal = Calendar.getInstance();
System.out.println("Before "+cal.getTime());  //Before Thu Jan 31 10:16:23 IST 2013

cal.add(Calendar.MONTH, 1);
System.out.println("After "+cal.getTime()); //After Thu Feb 28 10:16:23 IST 2013

Problem

I am using the following code ``` Calendar cal = Calendar.getInstance(); System.out.println("Before "+cal.getTime()); cal.set(Calendar.MONTH, 01); System.out.println("After "+cal.getTime()); ``` the output is ``` Before Thu Jan 31 10:07:34 IST 2013 After Sun Mar 03 10:07:34 IST 2013 ``` for adding `+1` to jan is giving mar month. may be it returning correct output if we add 30 days to present date. but i want to show feb month. can any body help me please..

Original source