如何根据日期选择器设置最大时间选择器限制 android studio

how to set max timepicker limit based on datepicker android studio

如何在特定日期设置限制最大时间选择器? 当我在日期选择器中选择星期六时,时间选择器将最大小时数设置为 12.00。但是如果我在另一天选择日期选择器,那么时间选择器将限制最大小时数设置为 15.00(正常)。我使用下面的代码但失败了,当我选择星期六时总是错误的。

   if( calendarToday.get( Calendar.DAY_OF_WEEK ) == Calendar.SATURDAY){

日期选择器()

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);
   int day     = calendarToday.get(Calendar.DAY_OF_MONTH);

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

   //sunday off
   List<Calendar> daysList = new LinkedList<>();
   Calendar[] daysArray;
   Calendar cAux = Calendar.getInstance();

   while( calendarToday.getTimeInMillis() <= calendarMax.getTimeInMillis() ){
       if( calendarToday.get( Calendar.DAY_OF_WEEK ) != Calendar.SUNDAY){
           Calendar c = Calendar.getInstance();
           c.setTimeInMillis( calendarToday.getTimeInMillis() );
           daysList.add( c );
       }
       calendarToday.setTimeInMillis( calendarToday.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);
   datePickerDialog.show( getFragmentManager(), "DatePickerDialog" );
}

onDataSet()

public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
   int bulan = (++monthOfYear);
   String day_string = String.valueOf(dayOfMonth);
   String month_string = String.valueOf(bulan);
   if (dayOfMonth < 10) {
      day_string = "0" + String.valueOf(dayOfMonth);
   }
   if (bulan < 10) {
      month_string = "0" + String.valueOf(bulan);
   }

   Calendar selectedDate =  Calendar.getInstance();
   int mYear   = selectedDate.get(Calendar.YEAR);
   int mMonth  = selectedDate.get(Calendar.MONTH);
   int mDay    = selectedDate.get(Calendar.DAY_OF_MONTH);
   String D    = String.valueOf(mDay);

   if(String.valueOf(dayOfMonth).equals(D)){    //if choose today
       if(selectedDate.get(Calendar.HOUR_OF_DAY)>6 && selectedDate.get(Calendar.HOUR_OF_DAY)<15) {
           if(selectedDate.get(Calendar.HOUR_OF_DAY)==14 && selectedDate.get(Calendar.MINUTE)>30) {
               input_time.setText("");
           } else {
               timePicker(true);
           }
       }else if(selectedDate.get(Calendar.HOUR_OF_DAY)>0 && selectedDate.get(Calendar.HOUR_OF_DAY)<7) {
           timePicker(true);
       }else{
           input_time.setText("");
       }
   }else{
       timePicker(false);
   }

   input_date.setText("");
   input_date.setText(day_string + "." + month_string + "." + year);
}

timePicker()

public void timePicker(boolean isTodayDate){
   int minHour = 8;
   int maxHour = 15;    //max hour, if saturday is 12, but other day, normal maxHour 15
   Calendar now = Calendar.getInstance();
   TimePickerDialog timepickerdialog = TimePickerDialog.newInstance(
        FormBooking.this,
        now.get(Calendar.HOUR_OF_DAY),
        now.get(Calendar.MINUTE),
        true);

   if(isTodayDate){ //if choose today
       if(now.get(Calendar.HOUR_OF_DAY)>6 && now.get(Calendar.HOUR_OF_DAY)<15){
           if(now.get(Calendar.MINUTE)>=0 && now.get(Calendar.MINUTE)<61) {
               minHour = now.get(Calendar.HOUR_OF_DAY) + 1;
           }
       }else if(now.get(Calendar.HOUR_OF_DAY)>0 && now.get(Calendar.HOUR_OF_DAY)<7){
           minHour = 8;
       }
   }

   timepickerdialog.setMinTime(minHour, 0, 0);
   timepickerdialog.setMaxTime(maxHour, 0, 0);

   timepickerdialog.setThemeDark(false);
   timepickerdialog.vibrate(false);
   timepickerdialog.dismissOnPause(false);
   timepickerdialog.show(getFragmentManager(), "Timepickerdialog");
}

您可以使用以下代码检查所选日期是否为星期六:

Calendar cal = Calendar.getInstance();
cal.set(Calendar.YEAR, year);
cal.set(Calendar.MONTH, monthOfYear);// you have increased value of monthOfYear by using ++monthOfYear so you have to use monthOfYear-1
cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
isSaturday = cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY;

并将此布尔值传递给您的 timePicker(false, isSaturday) 函数。在 timePicker 函数中,您可以设置 maxHours:

int maxHour = 15;
if(isSaturday){ 
    maxHour=12 
}