-1

I want the DatePickerDialog to not show dates before current date. I'm using the setMinDate(long l) method. But it's not working.

I use a inner class where I set the minDate:

public static class DatePickerFragment extends DialogFragment
        implements DatePickerDialog.OnDateSetListener {
    DatePickerDialog datePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        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);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        String s = month + day + year+"";
        datePickerDialog = new DatePickerDialog(getActivity(),this,year,month,day);
        datePickerDialog.getDatePicker().setMinDate(Long.parseLong(s));

        // Create a new instance of DatePickerDialog and return it
            return  datePickerDialog;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        SelectedDateView.setText(day+ (month + 1) + "-"  + "-" + year);

    }
}

This is the method that shows it:

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getActivity().getSupportFragmentManager(), "datePicker");
}
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • `Long.parseLong(s)` what do you think this does? – njzk2 Jan 29 '16 at 14:59
  • @njzk2 converts string to long? – Abhi Jan 29 '16 at 15:04
  • yes, but what string, to what date? what is `setMinDate` expecting? – njzk2 Jan 29 '16 at 15:16
  • @njzk2 I dont know what you're trying to say. `s = month + day+ year` is passed to `setMinDate` – Abhi Jan 29 '16 at 15:58
  • when in doubt, rtfm. In particular, the documentation for `setMinDate`. You should see that it is expecting a particular value, not what you think represents a date. – njzk2 Jan 29 '16 at 16:20
  • I don't really know what else to add. If you read the documentation, which @simon linked to and copied in his answer, you'll find that the long you need to pass is a number of millisecond since a certain date. Not something built arbitrarily from a string of days and months. If you simply google `datepicker` and `setmindate`, you find other questions, such as this one https://stackoverflow.com/questions/16859192/setmindate-for-datepicker that will give you examples of the kind of input that is expected. Or, you can read any of the answers given to your question. – njzk2 Jan 29 '16 at 17:17

3 Answers3

1

setMinDate() expects the unix time (milliseconds since January 1, 1970 00:00:00). Try something like this:

long currentTime = new Date().getTime();
datePickerDialog.getDatePicker().setMinDate(currentTime);

Hope this helps.

simon
  • 2,896
  • 1
  • 17
  • 22
  • 1
    as a side note, unix time is usually in seconds. java uses milliseconds because why not. – njzk2 Jan 29 '16 at 15:18
  • 1
    Hi..I tried this but in lollipop the datepicker appears like a calendar with the dates before today looking disabled but still selectable. On selecting a date I am setting it to a `TextView`. I am able to set the dates before today even though it looks disabled – Abhi Jan 30 '16 at 04:24
  • This seems to be a bug in Android Lollipop. Check whether the selected date is valid in your `onDateSet` method and only set it in your textview if it is. – simon Jan 31 '16 at 22:04
  • @spongeBob Late response, but I just ran across this issue. I also believe that it is a bug, and particularly with Android API 21 (earlier Lollipop). API levels before and after are not affected by the calendar bug. Note that this bug also affects `setMaxDate()`. Do as @simon says and add a redundant check to verify the picked date is within range before using it. – Tony Chan May 05 '16 at 21:21
0

Try this:

long time= System.currentTimeMillis();
datePickerDialog.getDatePicker().setMinDate(time);
Nicolas Cortell
  • 659
  • 4
  • 16
  • Hi..I tried this but in lollipop the datepicker appears like a calendar with the dates before today looking disabled but still selectable. On selecting a date I am setting it to a TextView. I am able to set the dates before today even though it looks disabled – Abhi Jan 30 '16 at 06:55
0

For sdks older than 23, you will have to use "System.currentTimeMillis()" instead of "cal.getTimeInMillis()".

sdk < 23: datePickerStart.getDatePicker().setMinDate(System.currentTimeMillis())
sdk > 23: datePickerStart.getDatePicker().setMinDate(cal.getTimeInMillis())