I have one activity that i use to create customer, in this activity i have one edit text that can show the current date when user open screen. its can be change to future date but not past date so i have used
d2.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
to set current date as a minimum date and onClick
i have opening DatePickerDialog
. But, when click on that edit text app is crashed and this error show in Logcat
.
java.lang.IllegalArgumentException: fromDate: Wed Aug 22 16:31:24 GMT+05:30 2018 does not precede toDate: Wed Aug 22 00:00:00 GMT+05:30 2018
here is my 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);
i used other solution like
d2.getDatePicker().setMinDate(System.currentTimeMillis());
or
d2.getDatePicker().setMinDate(new Date().getTime() - 10000);
but still app crash and same error comes
if i comment above line of code, my app run without crash.
you need to check if calendar date(cal.getTimeInMillis()) is greeter then your current date(System.currentTimeMillis())
like this
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());
}
may be this help