Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Hide Past dates and restrict user to select future dates only

How to Hide past dates on DatePicker dialog ? I don't wanna allow user to select past dates

What would be the best way ? if i want to restrict user to select future dates only !

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        .................................................
        editDate.setOnClickListener(new View.OnClickListener() {

            @SuppressWarnings("deprecation")
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DIALOG_DATE);
            }
        });   

    }

     @Override
        protected Dialog onCreateDialog(int id) {
            switch (id) {
            case DIALOG_DATE:
                return new DatePickerDialog(this, new OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker view, int year,
                            int monthOfYear, int dayOfMonth) {
                        dateTime.set(year, monthOfYear, dayOfMonth);
                        editDate.setText(dateFormatter
                                .format(dateTime.getTime()));
                    }
                }, dateTime.get(Calendar.YEAR),
                   dateTime.get(Calendar.MONTH),
                   dateTime.get(Calendar.DAY_OF_MONTH));
            }
            return null;
     }

}
like image 510
Sun Avatar asked Sep 17 '14 11:09

Sun


People also ask

How do I restrict dates on date picker?

You can restrict the users from selecting a date within the particular range by specifying MinDate and MaxDate properties. The default value of MinDate property is 1/1/1920 and MaxDate property is 12/31/2120 . Dates that appears outside the minimum and maximum date range will be disabled (blackout).

How do I hide past dates on calendar?

the previous dates we need to set the minDate property of the date picker. if we set minDate:0 then it will disable all the previous dates. and we set input attribute min:current_date then it will disable all the previous dates.

How would you restrict date picker selection based on the entry in another date field?

The requirement is also to be able to use dates in the format "dd-MMM-yy". For example, in the first date field, the user selects 26-Nov-16. Then, when they click on the date picker for the second date field, the earliest date they are able to select is 26 Nov.

How do I show only future dates in Datepicker?

Here is a complete solution where you can use <apex:input type="date" with jQuery Datepicker where property minDate set to 0 (to show only future dates). I used a normal html <input type="text" to show the jQuery Datepicker and hide the <apex:input type="date" field.


2 Answers

You have to use setMinDate for minimum date and you can also use setMaxDate if you want to set maximum date.

Here is sample code for SetMinDate for DatePickerDialog.

@Override
protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DATE:

            DatePickerDialog mDatePickerDialog = new DatePickerDialog(this, new OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                    dateTime.set(year, monthOfYear, dayOfMonth);
                    editDate.setText(dateFormatter.format(dateTime.getTime()));
                }
            }, dateTime.get(Calendar.YEAR), dateTime.get(Calendar.MONTH), dateTime.get(Calendar.DAY_OF_MONTH));
            Calendar mCalendarMinimum = Calendar.getInstance();
            mCalendarMinimum.add(Calendar.HOUR, -1);
            mDatePickerDialog.getDatePicker().setMinDate(mCalendarMinimum.getTimeInMillis());
            return mDatePickerDialog;
        }
        return null;
    }
like image 66
Niranj Patel Avatar answered Oct 23 '22 12:10

Niranj Patel


You can use setMinDate (long minDate) to achieve your goal. Doc says:

Sets the minimal date supported by this NumberPicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
like image 36
Alex Chengalan Avatar answered Oct 23 '22 12:10

Alex Chengalan