0

I am using DatePickerDialog in my activity ,

I want to set limit the date DatePickerDialog by user.

They should not able to select date more than current date.

My DatePickerDialog java code

edittodate.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            edittodate.setText("");

             final Calendar mcurrentDate=Calendar.getInstance();
               int mYear=mcurrentDate.get(Calendar.YEAR);
               int mMonth=mcurrentDate.get(Calendar.MONTH);
               int mDay=mcurrentDate.get(Calendar.DAY_OF_MONTH);
               final Date curDate= new Date();
                DatePickerDialog mDatePicker=new DatePickerDialog(ViewChallan.this, new OnDateSetListener() {                  
                    public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
                        // TODO Auto-generated method stub                      
                        /*      Your code   to get date and time    */
                        if(mcurrentDate.before(curDate))
                        {
                            Toast.makeText(context, "Invalid Date", Toast.LENGTH_LONG).show();
                        }
                        else
                        {
                            selectedmonth= selectedmonth+1;
                            edittodate.setText(selectedyear+"-"+selectedmonth+"-"+selectedday);
                        }


                    }
                },mYear, mMonth, mDay);
                mDatePicker.setTitle("Select date"); 
             // this code below is not hiding calendar,stopping the application
                if (Build.VERSION.SDK_INT >= 11) {
                    mDatePicker.getDatePicker().setCalendarViewShown(false);
                }
                mDatePicker.show(); 

        }
        });

Please Suggest me how can set limit in date

Thanks In Advance

Renu Singh
  • 111
  • 3
  • 10

2 Answers2

4

You have the setMinDate(long) and setMaxDate(long) methods at your disposal. Both of these will work on API level 11 and above. Since you are using a DatePickerDialog, you need to first get the underlying DatePicker by calling the getDatePicker() method.

dpdialog.getDatePicker().setMinDate(minDate);  
dpdialog.getDatePicker().setmaxDate(maxDate);  

Source: Set Limit on the DatePickerDialog in Android?

Edit:

Date today = new Date();
Calendar c = Calendar.getInstance();
c.setTime(today);
c.add( Calendar.MONTH, -6 ) // Subtract 6 months
long minDate = c.getTime().getTime() // Twice!  

Source: java.util.Date - Deleting three months from a date?

Community
  • 1
  • 1
An SO User
  • 24,612
  • 35
  • 133
  • 221
0

Setting minumum and maximum date for the DatePickerDialog is not enough. User can click on the year and select any other year.

You should also set year range on the DatePicker like this:

datePicker.setYearRange(calendar.get(Calendar.YEAR), calendar.get(Calendar.YEAR) + 1);
Oguz Ozcan
  • 1,694
  • 19
  • 26