Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to limit user on DatePicker to can only choose date before today_date - 18 years

How to limit on DatePicker to cannot choose date in future. I have DatePicker in activity which is used for registration. What to do so user cannot pick date of birthday if user has less that 18 years ( for example cannot pick date after today_date - 18 years )?

like image 473
Damir Avatar asked Feb 07 '12 10:02

Damir


2 Answers

You didn’t mention which API level. On Honeycomb and later, DatePicker has setMin/MaxDate methods you can use to restrict the permissible range.

like image 85
Lawrence D'Oliveiro Avatar answered Nov 15 '22 00:11

Lawrence D'Oliveiro


I hope you got the answer till now. Following might be useful to others as well: If you want to disable user to pick a date 18 years after today, here is what you may do.

Get time from 1-JAN-1970 to 18 years from now (in milli seconds)

   Calendar cal = Calendar.getInstance();
   cal.set(cal.get(Calendar.YEAR)+18,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH), 
            cal.get(Calendar.HOUR_OF_DAY), cal.get(Calendar.MINUTE), 0);
   long time = cal.getTimeInMillis();

set it as max allowed date in datepicker

   datepicker2.setMaxDate(time);

Hope it helps to anyone else looking for a answer

like image 23
Shobhit Puri Avatar answered Nov 14 '22 23:11

Shobhit Puri