Search code examples
javaandroiddatedatepickerandroid-datepicker

Date picker dialog date validation


I would like to implement the following behaviour:

  • When today's date is less than 15, 1-15 date is enabled in date Picker
  • When today's date is greater than 15, 16-31 date is enabled in date picker

My code is:

private void DateDialog() {
    final DatePickerDialog dpDialog = new DatePickerDialog(this,android.app.AlertDialog.THEME_DEVICE_DEFAULT_DARK,new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());
            //int curyear = cal.get(Calendar.YEAR);
            cal.set(i, i1, i2);
            etsubmstartdate.setText(dateformatter.format(cal.getTime()));
            selecteddate = dateformatter.format(cal.getTime());
            Log.e("Date of first edit text",selecteddate);
            etsubmstartdate.setText(selecteddate);
        }
    }, year, month, day);
    if (System.currentTimeMillis()<=15)
    {
        dpDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
        long now = System.currentTimeMillis() - 1000;
        dpDialog.getDatePicker().setMaxDate(now+(1000*60*60*24*15));
        dpDialog.show();
    }
    else if(System.currentTimeMillis()>=15)
    {
        dpDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000*60*60*24*2);
        long now = System.currentTimeMillis() - 1000*60*60*24*2;
        dpDialog.getDatePicker().setMaxDate(now+(1000*60*60*24*14));
        dpDialog.show();
    }
}

Solution

  • You can use .before and .after methods of Date class.

    For example,

    if (currentDate.before(yourdate)) {
    
    //write your code require to before current date
    }
    
    if (currentDate.after(yourdate)) {
    
    //write your code require to after current date
    }
    

    As per your code please replace below code in DateDialog() method

    private void DateDialog() {
    
     final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
    
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear,
                                              int dayOfMonth) {
                            // TODO Auto-generated method stub
                            myCalendar.set(Calendar.YEAR, year);
                            myCalendar.set(Calendar.MONTH, monthOfYear);
                            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                            updateLabel();
                        }
    
                    };
    
                    DatePickerDialog dpDialog = new DatePickerDialog(MainActivity.this, date, myCalendar
                            .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                            myCalendar.get(Calendar.DAY_OF_MONTH));
    
                    Toast.makeText(MainActivity.this, "date::"+myCalendar.getTimeInMillis(), Toast.LENGTH_SHORT).show();
    
                    String dt = "2018-04-15";
                    String dtSixteen = "2018-04-16";
    
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
                    try {
                        Date dateFifteen = format.parse(dt);
                        Calendar calFifteen = Calendar.getInstance();
                        calFifteen.setTime(dateFifteen);
    
                        Date dateSixteen = format.parse(dtSixteen);
                        Calendar calSixteen = Calendar.getInstance();
                        calSixteen.setTime(dateSixteen);
    
    
                        Date currentDate = myCalendar.getTime();
                        Calendar calendar = Calendar.getInstance();
    
                        calendar.setTime( myCalendar.getTime() );
                        calendar.set(Calendar.HOUR_OF_DAY, 0);
                        calendar.set(Calendar.MINUTE, 0);
                        calendar.set(Calendar.SECOND, 0);
                        calendar.set(Calendar.MILLISECOND, 0);
    
                        currentDate = calendar.getTime();
    
    
                        if(currentDate.before(dateFifteen) || currentDate.equals(dateFifteen)){
                            dpDialog.getDatePicker().setMaxDate(calFifteen.getTimeInMillis());
                        }else {
                             dpDialog.getDatePicker().setMinDate(calSixteen.getTimeInMillis());
                        }
    
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
    
                    dpDialog.show();
    
    }