I have 3 lines containing the values ββof day, month and year. For instance:
String mday = "02"; String mmonth="07"; String myear="2013";
I need to set DatePicker in my activity for a month from the above date. I don't mean just adding 1 to the millionth value ... in the case of the 31st day, I get an invalid date. So I need to somehow increase the date (the actual path) and set the DatePicker value with its value. I know that setting datePicker with Int values ββis done as follows:
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); datepicker.init(iYear, iMonth, iDay, null);
But how do I get the integer values ββof the day, month and year of the incremented DATE by one month?
So, between the first values ββ(strings) and the final values ββof the added date (integers), what steps should I follow?
I guess I will have to use Calendar. So my code should look like this:
Integer iYear, iMonth, iDay = 0; String mday = "02"; String mmonth="07"; String myear="2013"; Calendar cal = Calendar.getInstance(); cal.set(Integer.parseInt(myear), Integer.parseInt(mmonth), Integer.parseInt(mday)); cal.add(Calendar.MONTH, 1);
if a:
datepicker.init(cal.YEAR, cal.MONTH, cal.DATE, null);
then the application crashes. What should I do? How to set this step per month on my DatePicker?
UPDATE I changed my test code to this:
Calendar cal = Calendar.getInstance(); cal.set(2013, 05, 23); cal.add(Calendar.MONTH, 1); int xxday = cal.get(Calendar.DATE); int xxmonth = cal.get(Calendar.MONTH); int xxyear = cal.get(Calendar.YEAR); datepicker.init(xxyear, xxmonth, xxday, null);
but now datePicker is set to one month from NOW instead of one month from the desired date. Therefore, instead of (2013-06-23) I have (2013-09-23). I guess this is due
int xxmonth = cal.get(Calendar.MONTH);
how can i get the real month from cal calendar;