-2

Hi I implemented a DatePickerDialog but I want to set range, I found setMinDate and setMaxDate, but I don't know how to put values. I want to set the calendar for a user of min age 18 and max age 100; I think it's about calculations into milliseconds.

    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT, this, year, month, day);
dpd.getDatePicker().setMaxDate(max);
return dpd;

I don't know what value I have to put to max to do 2016 - 18.

Ben
  • 761
  • 1
  • 12
  • 35
  • http://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android – Juvi Apr 21 '16 at 11:50
  • @Juvi Yes I looked at, but it get the actual date as max – Ben Apr 21 '16 at 11:53
  • 1
    new Date().getTime() returns the current date, so i assume you want to set your own date. Just create new Date object and set it values, and than set it as maxDate... – Juvi Apr 21 '16 at 11:56
  • @Juvi I wan't that the user is not younger than 18 years old, soI have to set the max to now - 18 – Ben Apr 21 '16 at 12:00
  • @PoojaGaikwad Look my edit please – Ben Apr 21 '16 at 12:11
  • Possible duplicate of [Android datepicker min max date before api level 11](http://stackoverflow.com/questions/10836679/android-datepicker-min-max-date-before-api-level-11) – Anand Savjani Apr 21 '16 at 12:19
  • please visit this link will help you http://stackoverflow.com/questions/16749361/how-set-maximum-date-in-datepicker-dialog-in-android?lq=1 – Out Of Bounds Apr 21 '16 at 12:44

2 Answers2

1

I guess this is what you need:

            final Calendar maxc = Calendar.getInstance();
            int year = maxc.get(Calendar.YEAR);
            maxc.set(Calendar.YEAR,year-18);
            datePickerDialog.getDatePicker().setMaxDate(maxc.getTimeInMillis());

            final Calendar minc = Calendar.getInstance();
            int year = minc.get(Calendar.YEAR);
            minc.set(Calendar.YEAR,year-100);
            datePickerDialog.getDatePicker().setMinDate(minc.getTimeInMillis());
            datePickerDialog.show();
sanedroid
  • 1,036
  • 4
  • 16
  • 27
1

Use this code, to set maximum date and minimum date from current date.

Calendar minCal = Calendar.getInstance();
minCal.set(Calendar.YEAR, minCal.get(Calendar.YEAR) - 100);
Calendar maxCal = Calendar.getInstance();
maxCal.set(Calendar.YEAR, maxCal.get(Calendar.YEAR) - 18);
dialog.getDatePicker().setMinDate(minCal.getTimeInMillis());
dialog.getDatePicker().setMaxDate(maxCal.getTimeInMillis());
Krishna V
  • 1,801
  • 1
  • 14
  • 16
  • I don't get the good ranges it have to be between 1916 to 1998 (min age is 18 max age is 100) – Ben Apr 21 '16 at 12:37
  • Good thanks, do you know how I show the full date, like if I put 1/1/1993 I want to have 01/01/1993 – Ben Apr 21 '16 at 13:17