如何将“todate picker”选定日期设置为“from datepicker”日期的最大日期

时间:2017-11-24 10:56:10

标签: java android datepicker calendar

我有两个日期选择器From and To,在这里我可以将From datepicker值设置为to datepicker的最小日期。

现在我的要求是,如果我选择To datepicker日期,我想将其设置为From datepicker date的最大日期。我尝试了一种解决方案,但它不适用于我的情况。

private String fromDate;
private DatePickerDialog fromDatePicker;
private DatePickerDialog.OnDateSetListener fromDatePickerListener;
private Calendar fromDateCalendar;
private String toDate;
private DatePickerDialog toDatePicker;
private DatePickerDialog.OnDateSetListener toDatePickerListener;
private Calendar toDateCalendar;

Fromdatepicker OnDateSetListener方法

 fromDateCalendar = Calendar.getInstance(); 
    fromDatePickerListener = new DatePickerDialog.OnDateSetListener() { 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub 
        fromDateCalendar.set(Calendar.YEAR, year);
        fromDateCalendar.set(Calendar.MONTH, monthOfYear);
        fromDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        fromDate = fromDateCalendar.get(Calendar.YEAR) + "-" + 
  (fromDateCalendar.get(Calendar.MONTH)+1) + "-" + 
   fromDateCalendar.get(Calendar.DAY_OF_MONTH);
         textviewFrom.setText(fromDate); 

toDatePicker.getDatePicker().setMinDate(fromDateCalendar.getTimeInMillis()); 
    } 

}; 

to datepicker OnDateSetListener方法

 toDateCalendar = Calendar.getInstance();
 toDatePickerListener = new DatePickerDialog.OnDateSetListener() {

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,
                          int dayOfMonth) {
        // TODO Auto-generated method stub 
        toDateCalendar.set(Calendar.YEAR, year);
        toDateCalendar.set(Calendar.MONTH, monthOfYear);
        toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

        toDate = toDateCalendar.get(Calendar.YEAR) + "-" + 
 (toDateCalendar.get(Calendar.MONTH)+1) + "-" + 
  toDateCalendar.get(Calendar.DAY_OF_MONTH);
        textviewTo.setText(toDate); 
todatepicker.getDatePicker().setMinDate(-2208902400000L);
fromdatePicker.getDatePicker().setMaxDate(toDateCalendar.getTimeInMillis());

    } 

 }; 


toDatePicker = new DatePickerDialog(getContext(),toDatePickerListener, 
toDateCalendar.get(Calendar.YEAR), toDateCalendar.get(Calendar.MONTH), 
toDateCalendar.get(Calendar.DAY_OF_MONTH));

fromDatePicker = new DatePickerDialog(getContext(),fromDatePickerListener, 
fromDateCalendar.get(Calendar.YEAR), fromDateCalendar.get(Calendar.MONTH), 
fromDateCalendar.get(Calendar.DAY_OF_MONTH));

调用datepicker对话框的Textview列表器

textviewFrom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            fromDatePicker.show();
        }
    });
    textviewTo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            toDatePicker.show();
        }
    });

我试过

fromDatePicker.getDatePicker().setMaxDate(toDateCalendar.get‌​TimeInMillis()); 

在toDatePickerListener的onDateSet方法中,但不是    工作

2 个答案:

答案 0 :(得分:1)

我尝试过以下方式,希望它可以帮到你。

 private String selectedToDate = "";
 private String selectedFromDate = "";
 private Calendar calToDate, calFromDate;

将此代码写入onCreate()方法

calFromDate = convertToCalender(selectedFromDate);
calToDate = convertToCalender(selectedToDate);

Todate和FromDate TextView点击次数

textviewFrom.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

             showFromDatePicker(fromdateListener, calFromDate, calToDate);
        }
    });
textviewTo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            showToDatePicker(todateListener, calToDate, calFromDate);
        }
    });

toDate和fromDate Listener

 public DatePickerDialog.OnDateSetListener fromdateListener =
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    calFromDate.set(year, monthOfYear, dayOfMonth);
                    selectedFromDate = returnFormattedDate(calFromDate.getTimeInMillis());
                    textviewFrom.setText(selectedFromDate);
                }
            };

    DatePickerDialog.OnDateSetListener todateListener =
            new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int year, int monthOfYear,
                                      int dayOfMonth) {
                    calToDate.set(year, monthOfYear, dayOfMonth);
                    selectedToDate = returnFormattedDate(calToDate.getTimeInMillis());
                    textviewTo.setText(selectedToDate);
                }
};
private  String returnFormattedDate(long timeInMilliSec) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd",
                Locale.getDefault());
        return dateFormat.format(new Date(timeInMilliSec));
}

显示DatePicker的方法

private void showFromDatePicker(DatePickerDialog.OnDateSetListener listener,
                                Calendar calendar,
                                Calendar minDateCalender) {
        DatePickerDialog dialog = new DatePickerDialog(getActivity(),
                listener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
        if (minDateCalender != null) {
            dialog.getDatePicker().setMinDate(minDateCalender.getTimeInMillis());
        }
        dialog.show();
    }

private void showToDatePicker(DatePickerDialog.OnDateSetListener listener,
                                Calendar calendar,
                                Calendar maxDateCalender) {
        DatePickerDialog dialog = new DatePickerDialog(getActivity(),
                listener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH));
       dialog.getDatePicker().setMaxDate(maxDateCalender.getTimeInMillis());

        dialog.show();
    }

将字符串日期转换为日历

的方法
private Calendar convertToCalender(String date) {
        Calendar mCalender = Calendar.getInstance();
        if (date == null) {
            return mCalender;
        } else {
            try {
                DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
                Date returnDate = formatter.parse(date);
                mCalender = Calendar.getInstance();
                mCalender.setTime(returnDate);
                return mCalender;
            } catch (ParseException e) {
                e.printStackTrace();
                return mCalender;
            }
        }
}

答案 1 :(得分:0)

如果您在xml中使用DatePicker小部件,则以下内容将起作用。

toDatePickerListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                              int dayOfMonth) {
            // TODO Auto-generated method stub
            toDateCalendar.set(Calendar.YEAR, year);
            toDateCalendar.set(Calendar.MONTH, monthOfYear);
            toDateCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

            toDate = toDateCalendar.get(Calendar.YEAR) + "-" +
                    (toDateCalendar.get(Calendar.MONTH)+1) + "-" +
                    toDateCalendar.get(Calendar.DAY_OF_MONTH);
            fromdatePicker.setMaxDate(toDateCalendar.getTimeInMillis());
            //etToDate.setText(toDate);
        }

    };

或者如果您在另一个之后打开DatePickerDialog。然后,您应该保存先前选择的日期,并在调用show之前将其传递给下一个DatePickerDialog。

pickerDialog.getDatePicker().setMaxDate(maxTime);// Set Max and min here
pickerDialog.show();