禁用所有日期几个月后,从某个日期Android日期picker

本文关键字:日期 picker Android 几个月 | 更新日期: 2023-09-27 17:49:47

我有两个DatePickers在我的应用程序。首先,显示FromDate是CurrentDate+1,第二个显示ToDate ie。FromDate+1

我已经得到了上述场景的工作。但现在我想禁用所有日期6个月后从FromDate。我尝试了以下操作,但是日历显示01/01/1970为当前日期。

BookingActivity.cs

void IbtnToDate_Click(object sender, EventArgs e)
        {
            toDateClicked = true;
            fromDateClicked = false;
            dateFragment = new DatePickerFragment(this,  DateTime.Parse (editFromDate.Text.ToString ()).AddDays (1), this, DateTime.Parse (editFromDate.Text.ToString ()));
            dateFragment.Show(FragmentManager, null);
        }
        void IbtnFromDate_Click(object sender, EventArgs e)
        {
            fromDateClicked = true;
            toDateClicked = false;
            dateFragment = new DatePickerFragment(this, date.AddDays (1), this, date.AddDays (1));
            dateFragment.Show(FragmentManager, null);
        }

        public void OnDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            var date1 = new DateTime(year, monthOfYear + 1, dayOfMonth);
            if (fromDateClicked)
                UpdateFromDate (date1);
            else if (toDateClicked) {
                UpdateToDate (date1);
            }
        }

DatePickerFragment.cs

 public class DatePickerFragment : DialogFragment
    {
        private readonly Context _context;
        private DateTime _date, _minDate, _maxDate;
        private readonly Android.App.DatePickerDialog.IOnDateSetListener _listener;
        public DatePickerFragment(Context context, DateTime date, Android.App.DatePickerDialog.IOnDateSetListener listener, DateTime minDate)
        {
            _context = context;
            _date = date;
            _listener = listener;
            _minDate = minDate;
        }
        public override Dialog OnCreateDialog(Bundle savedState)
        {
            var dialog = new Android.App.DatePickerDialog(_context, _listener, _date.Year, _date.Month - 1, _date.Day);
            dialog.DatePicker.MinDate =_minDate.AddDays (1).Millisecond;
            dialog.DatePicker.MaxDate = _date.AddMonths(6).Millisecond;
            return dialog;
        }
    }

我怎样才能实现它?

禁用所有日期几个月后,从某个日期Android日期picker

您可以使用setMaxDate,并可以这样做:

1-从_mindate字符串中获取日期:

  String dateString = "03/26/2012";
    SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
    Date convertedDate = new Date();
    try {
        convertedDate = dateFormat.parse(dateString);
    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

2-从min_date:

获取6个月的日期
Calendar cal = GregorianCalendar.getInstance();
cal.setTime(convertedDate);
cal.add(Calendar.MONTH, 1);
Date 6MonthsFromCurrentDate = cal.getTime();

3-将datePicker限制为该日期:

datePickerDialog.getDatePicker().setMaxDate(6MonthsFromCurrentDate.getTime());

使用这些行代码。

        Calendar ci = Calendar.getInstance();
        String CiDateTime = ci.get(Calendar.YEAR) + "-" + 
            (ci.get(Calendar.MONTH) + 7) + "-" + //for 6 month future
            ci.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy");
        Date d;
        try {
            d = f.parse(CiDateTime);
             long milliseconds = d.getTime();
            _date.setMaxDate(milliseconds);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }