How to set minimum DatePicker date to current date

前端 未结 9 1671
有刺的猬
有刺的猬 2020-11-28 07:30

I want to set the minimum date the user can choose in a DatePicker to the current date. I\'ve tried this:

DatePicker datePicker = (DatePicker) findViewById(R         


        
相关标签:
9条回答
  • 2020-11-28 07:43

    Try this

    datePicker.setMinDate(new GregorianCalendar.getTimeInMillis());
    
    0 讨论(0)
  • 2020-11-28 07:44

    The error says you cannot set the minimum date to exactly now. Try subtracting a second:

    datePicker.setMinDate(System.currentTimeMillis() - 1000);
    

    From the source code the minimum date must be before, not equal to, the current date:

    if (date.before(mMinDate)) {
        throw new IllegalArgumentException("fromDate: " + mMinDate.getTime()
                + " does not precede toDate: " + date.getTime());
    }
    

    So you simply need to subtract enough time from now (System.currentTimeMillis()) pass date.before(mMinDate).

    0 讨论(0)
  • 2020-11-28 07:46

    This can help you.

    Calendar cal=Calendar.getInstance();
    
    int year=cal.get(Calendar.YEAR);
    int month=cal.get(Calendar.MONTH);
    int day=cal.get(Calendar.DAY_OF_MONTH);
    int hour=cal.get(Calendar.HOUR_OF_DAY);
    int min=cal.get(Calendar.MINUTE);
    
    datePicker.updateDate(year, month, day);
    
    timePicker.setCurrentHour(hour);
    timePicker.setCurrentMinute(min);
    
    0 讨论(0)
  • 2020-11-28 07:48

    Check the Android DatePickerDialog set minimum and maximum date code.

    Here is the examples.

    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_TRADITIONAL,this,year, month, day);  
    
        //Get the DatePicker instance from DatePickerDialog  
        DatePicker dp = dpd.getDatePicker();  
        //Set the DatePicker minimum date selection to current date  
        dp.setMinDate(c.getTimeInMillis());//get the current day  
        //dp.setMinDate(System.currentTimeMillis() - 1000);// Alternate way to get the current day  
    
        //Add 6 days with current date  
        c.add(Calendar.DAY_OF_MONTH,6);  
    
        //Set the maximum date to select from DatePickerDialog  
        dp.setMaxDate(c.getTimeInMillis());  
        //Now DatePickerDialog have 7 days range to get selection any one from those dates 
    
    0 讨论(0)
  • 2020-11-28 07:58

    This worked perfectly for me. easy and nice.

    DatePickerDialog dialog = new DatePickerDialog(this, this,
                  Calendar.YEAR,Calendar.MONTH,
                    Calendar.DAY_OF_MONTH);
    
            dialog.getDatePicker().setMinDate(Calendar.getInstance().getTimeInMillis());
            dialog.show();
    
    0 讨论(0)
  • 2020-11-28 08:01
    datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    

    The minimum date can't be the instant moment

    0 讨论(0)
提交回复
热议问题