我想在Android中禁用DateTimePicker中的假期,为此我创建了一个假日数组,并将其与循环中的日历日期进行比较,但它无效。
这是我的代码: 公共类MainActivity扩展AppCompatActivity实现DatePickerDialog.OnDateSetListener,TimePickerDialog.OnTimeSetListener,DialogInterface.OnCancelListener {
private TextView textView;
private static final String[] bankHolidays = {"2017/09/25","2017/09/26","2017/09/27","2017/09/28"};
private static final java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/mm/dd");
private static Set<java.util.Calendar> holidayDates;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.tv_datepicker);
textView.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
try {
schedule(v);
} catch (ParseException e) {
e.printStackTrace();
}
}
});
}
private int year,month,day,hour,minute;
@RequiresApi(api = Build.VERSION_CODES.N)
public void schedule(View v) throws ParseException {
if (holidayDates == null) {
holidayDates = new HashSet<java.util.Calendar>();
for (int i = 0; i < bankHolidays.length; i++) {
java.util.Calendar c = java.util.Calendar.getInstance();
Date date = sdf.parse(bankHolidays[i]);
c.setTime(date);
holidayDates.add(c);
}
}
Toast.makeText(getApplicationContext(),"welcome",Toast.LENGTH_LONG).show();
intDatetimeData();
java.util.Calendar cDefault =java.util.Calendar.getInstance();
cDefault.set(year,month,day);
DatePickerDialog datePickerDialog=DatePickerDialog.newInstance(this,
cDefault.get(java.util.Calendar.YEAR),
cDefault.get(java.util.Calendar.MONTH),
cDefault.get(java.util.Calendar.DAY_OF_MONTH)
);
java.util.Calendar cMax = java.util.Calendar.getInstance();
java.util.Calendar cMin = java.util.Calendar.getInstance();
cMax.set(cMax.get(java.util.Calendar.YEAR),11,31);
datePickerDialog.setMaxDate(cMin);
datePickerDialog.setMaxDate(cMax);
List<java.util.Calendar> daylist = new LinkedList<>();
java.util.Calendar[] daysArray;
java.util.Calendar cAux = java.util.Calendar.getInstance();
while (cAux.getTimeInMillis() <= cMax.getTimeInMillis() )
{
if ( cAux.get(java.util.Calendar.DAY_OF_WEEK) != java.util.Calendar.SATURDAY && !holidayDates.contains(cAux.get(java.util.Calendar.DAY_OF_YEAR))){
java.util.Calendar c = java.util.Calendar.getInstance();
c.setTimeInMillis(cAux.getTimeInMillis());
daylist.add(c);
}
cAux.setTimeInMillis(cAux.getTimeInMillis() + (24 * 60 * 60 * 1000));
}
daysArray= new java.util.Calendar[daylist.size()];
for(int i=0; i<daysArray.length;i++)
{
daysArray[i]=daylist.get(i);
}
datePickerDialog.setSelectableDays(daysArray);
datePickerDialog.setOnCancelListener(this);
datePickerDialog.show(getFragmentManager(),"datePickerDialog");
}