Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/217.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在DatePickerDialog中将当前日期设置为MinDate_Android_Calendar_Datepickerdialog - Fatal编程技术网

Android 如何在DatePickerDialog中将当前日期设置为MinDate

Android 如何在DatePickerDialog中将当前日期设置为MinDate,android,calendar,datepickerdialog,Android,Calendar,Datepickerdialog,我有一个用于创建客户的活动,在这个活动中,我有一个编辑文本,可以在用户打开屏幕时显示当前日期。它可以更改为未来日期,但不能更改为过去日期,所以我使用了 d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 要将当前日期设置为最短日期,请单击onClicki打开DatePickerDialog。但是,当点击编辑文本应用程序时崩溃,此错误显示在Logcat中 java.lang.IllegalArgumentExcept

我有一个用于创建客户的活动,在这个活动中,我有一个编辑文本,可以在用户打开屏幕时显示当前日期。它可以更改为未来日期,但不能更改为过去日期,所以我使用了

d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
要将当前日期设置为最短日期,请单击
onClick
i打开
DatePickerDialog
。但是,当点击编辑文本应用程序时崩溃,此错误显示在
Logcat

java.lang.IllegalArgumentException:fromDate:Wed Aug 22 16:31:24 2018年格林尼治标准时间+05:30不在今天之前:8月22日星期三00:00:00格林尼治标准时间+05:30 2018年

这是我的edittext
onClickListner
code

Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.dateFormatForDOB);
try {
    cal.setTime(sdf.parse(edt.getText().toString()));
} catch (ParseException e) {
    e.printStackTrace();
}

DatePickerDialog d2 = new DatePickerDialog(CreateUpdateOpportunityActivity.this, android.R.style.Theme_Holo_Light_Dialog_NoActionBar_MinWidth,
     new DatePickerDialog.OnDateSetListener() {
                @Override
                public void onDateSet(DatePicker view, int years,
                                      int monthOfYear, int dayOfMonth) {
                    year = years;
                    month = monthOfYear;
                    day = dayOfMonth;
                    tv.setText(DateUtil.getDateOfBirth(year + "-" + (month + 1) + "-" + day));
                }
            }, year, month, day);

d2.getDatePicker().setSpinnersShown(true);
d2.getDatePicker().setCalendarViewShown(false);
d2.updateDate(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
d2.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
d2.show();
d2.setCanceledOnTouchOutside(false);
我使用了其他解决方案,比如

d2.getDatePicker().setMinDate(System.currentTimeMillis());

但应用程序仍然会崩溃,同样的错误也会出现


如果我在代码行上方添加注释,我的应用程序运行时不会崩溃。

您需要检查日历日期(cal.GetTimeMillis())是否比当前日期(System.currentTimeMillis())更合适

像这样

 if (cal.getTimeInMillis() < System.currentTimeMillis()) {
        // Toast.makeText(getApplicationContext(),"Current time is big",Toast.LENGTH_SHORT).show();
        d2.getDatePicker().setMinDate(cal.getTimeInMillis());
    } else {
        //  Toast.makeText(getApplicationContext(),"Current time is small",Toast.LENGTH_SHORT).show();
        d2.getDatePicker().setMinDate(System.currentTimeMillis());
    }
if(cal.getTimeInMillis()

这可能有帮助吗

什么是DateUtil.dateFormatForDOB?当用户打开屏幕时作为默认设置…我获取当前日期并直接在edittext中设置。。。即当前日期格式公共静态最终字符串dateFormatForDOB=“MM/dd/yyyy”@MayurKarmur,请尝试此代码并检查(cal.getTimeInMillis()>System.currentTimeMillis()){d2.getDatePicker().setMinDate(System.currentTimeMillis()-1000);}谢谢…其他部分呢?
 if (cal.getTimeInMillis() < System.currentTimeMillis()) {
        // Toast.makeText(getApplicationContext(),"Current time is big",Toast.LENGTH_SHORT).show();
        d2.getDatePicker().setMinDate(cal.getTimeInMillis());
    } else {
        //  Toast.makeText(getApplicationContext(),"Current time is small",Toast.LENGTH_SHORT).show();
        d2.getDatePicker().setMinDate(System.currentTimeMillis());
    }