如何在datepicker android studio中设置活动日期tommorow

时间:2018-02-05 04:42:18

标签: android android-studio datepicker

如何在datepicker android studio中设置活动日期tommorow?我今天会禁用日期,并且活动日期是tommorow吗?

Calendar cMin = Calendar.getInstance();
Calendar cMax = Calendar.getInstance();

cMax.add(Calendar.DAY_OF_MONTH, 7); //active in 7 days
datePickerDialog.setMinDate(cMin); //active today
datePickerDialog.setMaxDate(cMax); //max active in 7 days

今天如何禁用?

我的所有代码都要更详细,但如果禁用星期日,今天会启用,如何处理这个问题?

public void datePicker(){
    final Calendar calendarMax = Calendar.getInstance();
    calendarMax.add(Calendar.DAY_OF_MONTH, 7);

    final Calendar calendarToday = Calendar.getInstance();
    int year    = calendarToday.get(Calendar.YEAR);
    int month   = calendarToday.get(Calendar.MONTH);
    calendarToday.add(Calendar.DAY_OF_MONTH, 1);
    int day     = calendarToday.get(Calendar.DAY_OF_MONTH);

    DatePickerDialog datePickerDialog = DatePickerDialog.newInstance(FormTestDrive.this, year, month, day);
    datePickerDialog.setMinDate(calendarToday);
    datePickerDialog.setMaxDate(calendarMax);

    //membuat hari minggu tidak dapat di pilih
    List<Calendar> daysList = new LinkedList<>();
    Calendar[] daysArray;
    Calendar cAux = Calendar.getInstance();

    while( cAux.getTimeInMillis() <= calendarMax.getTimeInMillis() ){
        //if( cAux.get( Calendar.DAY_OF_WEEK ) != 1 && cAux.get( Calendar.DAY_OF_WEEK ) != 7 ){
        if( cAux.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY){
            Calendar c = Calendar.getInstance();
            c.setTimeInMillis( cAux.getTimeInMillis() );
            daysList.add( c);
        }
        cAux.setTimeInMillis( cAux.getTimeInMillis() + ( 24 * 60 * 60 * 1000 ) );
    }
    daysArray = new Calendar[ daysList.size() ];
    for( int i = 0; i < daysArray.length; i++ ){
        daysArray[i] = daysList.get(i);
    }
    datePickerDialog.setSelectableDays( daysArray );

    datePickerDialog.vibrate(true); //vibrate on choosing date?
    datePickerDialog.show( getFragmentManager(), "DatePickerDialog" );
}

2 个答案:

答案 0 :(得分:1)

试试这个:

    final Calendar calendarMax = Calendar.getInstance();
    calendarMax.add(Calendar.DAY_OF_MONTH, 7);

    final Calendar calendarToday = Calendar.getInstance();
    int year = calendarToday.get(Calendar.YEAR);
    int month = calendarToday.get(Calendar.MONTH);
    calendarToday.add(Calendar.DAY_OF_MONTH, 1);
    int day = calendarToday.get(Calendar.DAY_OF_MONTH);


    DatePickerDialog datePickerDialog = new DatePickerDialog(DashBoardActivity.this,
            null,
            year, month, day);
    datePickerDialog.getDatePicker().setMinDate(calendarToday.getTimeInMillis());
    datePickerDialog.getDatePicker().setMaxDate(calendarMax.getTimeInMillis());
    datePickerDialog.show();

<强> O / P

output

希望这有助于你...如果你需要任何帮助,你可以问

答案 1 :(得分:0)

试试这种方式

Calendar cal = Calendar.getInstance(); 
cal.setTime(TODAY); 
cal.add(Calendar.DAY_OF_MONTH, 1); //Adds a day
cal.add(Calendar.DAY_OF_MONTH, -1); //Goes to previous day
yourDate = cal.getTime();