5

I use DatePickerDialog.OnDateSetListener that's works fine.

I want to add date for 120 days in date picker.

What I mean is if I add 120 days, the date and month will be change automatically. How to do it?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
shivcena
  • 2,023
  • 8
  • 24
  • 50
  • Get the date form the date picker into a variable of type `Calendar`. Add 120 days to it, then set the new date back into the DatePicker. – Aleks G Aug 13 '12 at 12:48
  • ya , i get value in calender as Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH);but how to add 120 days from it – shivcena Aug 13 '12 at 12:50
  • c.add(Calendar.DAY_OF_YEAR, 120); – Will Kru Aug 13 '12 at 12:52
  • I posted an answer that, I hope, addresses what you are asking for. – Aleks G Aug 13 '12 at 12:55
  • This was asked before.. here is the link http://stackoverflow.com/questions/6421874/how-to-get-the-date-set-in-the-datepicker-widget-in-android – Crazyshezy Aug 13 '12 at 12:55

3 Answers3

11

Something like this should do the trick:

Calendar cal = Calendar.getInstance();
cal.set(datepick.getYear(), datepick.getMonth() + 1, datepick.getDayOfMonth());
cal.add(Calendar.DATE, 120);
datepick.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) - 1, cal.get(Calendar.DATE));
Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

Make sure you create a date object with 120 days added (see this topic on how to do that) and use that to populate your datepicker, either on initialization or when changed. I'm not really sure what you are trying to achieve however the latter doesn't seem right usability wise. In that case I would create an extra textfield that represents the +120 days date.

Community
  • 1
  • 1
Will Kru
  • 5,164
  • 3
  • 28
  • 41
0

Set Date Programmatically by using UpdateDate

datePickerDialog.UpdateDate(selectedDate ?? DateTime.Now);
logeshpalani31
  • 1,416
  • 12
  • 33