Android set DatePicker to a specific date

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); // where iYear,iMonth and iDay are integers 

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); // here I should get the values from cal inside the iYear, iMonth, iDay, but I do not seem to succeed. DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker1); datepicker.init(iYear, iMonth, iDay, null); 

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;

+4
source share
4 answers

DatePicker class has an updateDate(year, month, dayOfMonth) , which you can use to set the date in DatePicker , as shown below:

 DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1); datePicker.updateDate(2016, 5, 22); 
+6
source

The calendar month is based on 0. So month 07 is August.

+2
source

Use the following code to initialize the calendar object if you have a date picker:

 Calendar calendar = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth()); 

Another hard code: date details in the constructor

+2
source

use this tattoo to create a DatePickerDialog, then use this code inside DatePickerDialog https://developer.android.com/guide/topics/ui/dialogs.html

 @Override public Dialog onCreateDialog(Bundle savedInstanceState) { String mday = "02"; String mmonth="07"; String myear="2013"; //convert them to int int mDay=Integer.valueOf(mday); int mMonth=Integer.valueOf(mmonth); int mYear=Integer.valueOf(myear); return new DatePickerDialog(getActivity(), new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker datePicker, int i, int i1, int i2) { String d=convertToCompletDate(i2,i1,i); mListener.onDatePicked(d); } },mYear,mMonth,mDay); } 
0
source

Source: https://habr.com/ru/post/1495052/


All Articles