Android Show The Datepicker(end Date) Greater Than The Start Date?
Solution 1:
fromdate = (EditText) findViewById(fromDate);
todate = (EditText) findViewById(R.id.todate);
fromdate.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
showTruitonDatePickerDialog(view);
}
});
todate.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View view) {
showToDatePickerDialog(view);
}
});
}
publicvoidshowTruitonDatePickerDialog(View v) {
DialogFragmentnewFragment=newDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
publicvoidshowToDatePickerDialog(View v) {
DialogFragmentnewFragment=newToDatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
publicstaticclassDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the pickerfinalCalendarc= Calendar.getInstance();
intyear= c.get(Calendar.YEAR);
intmonth= c.get(Calendar.MONTH);
intday= c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog;
datePickerDialog = newDatePickerDialog(getActivity(),this, year,
month,day);
return datePickerDialog;
}
publicvoidonDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
fromdate.setText(day + "/" + month + "/" + year);
}
}
publicstaticclassToDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {
// Calendar startDateCalendar=Calendar.getInstance();@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the pickerStringgetfromdate= fromdate.getText().toString().trim();
String getfrom[] = getfromdate.split("/");
int year,month,day;
year= Integer.parseInt(getfrom[2]);
month = Integer.parseInt(getfrom[1]);
day = Integer.parseInt(getfrom[0]);
finalCalendarc= Calendar.getInstance();
c.set(year,month,day+1);
DatePickerDialogdatePickerDialog=newDatePickerDialog(getActivity(),this, year,month,day);
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
return datePickerDialog;
}
publicvoidonDateSet(DatePicker view, int year, int month, int day) {
todate.setText(day + "/" + month + "/" + year);
}
}
I choose a start date as....1/09/2017
Then i open a end date it displays from....2/09/2017
Solution 2:
try to use this code
publicstaticbooleanisDateAfter(String startDate,String endDate)
{
try
{
String myFormatString = "yyyy-M-dd"; // for exampleSimpleDateFormat df = newSimpleDateFormat(myFormatString);
Date date1 = df.parse(endDate));
Date startingDate = df.parse(startDate);
if (date1.after(startingDate))
returntrue;
elsereturnfalse;
}
catch (Exception e)
{
returnfalse;
}
}
It return true if enddate is after start date.
Solution 3:
Define startDateCalendar like
Calendar startDateCalendar=Callendar.getInstance();
And update fragments code
publicstaticclassDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the pickerfinalCalendarc= Calendar.getInstance();
intyear= c.get(Calendar.YEAR);
intmonth= c.get(Calendar.MONTH);
intday= c.get(Calendar.DAY_OF_MONTH);
DatePickerDialogdatePickerDialog=newDatePickerDialog(mActivity,this, year, month,day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
// Create a new instance of DatePickerDialog and return itreturn datePickerDialog;
}
publicvoidonDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
fromdate.setText(day + "/" + (month + 1) + "/" + year);
startDateCalendar.set(Calendar.YEAR, year);
startDateCalendar.set(Calendar.MONTH, month);
startDateCalendar.set(Calendar.DAY_OF_MONTH, day);
} }
publicstaticclassToDatePickerFragmentextendsDialogFragmentimplementsDatePickerDialog.OnDateSetListener {
@Overridepublic Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the pickerfinalCalendarc= Calendar.getInstance();
intyear= c.get(Calendar.YEAR);
intmonth= c.get(Calendar.MONTH);
intday= c.get(Calendar.DAY_OF_MONTH);
DatePickerDialogdatePickerDialog=newDatePickerDialog(getActivity(),this, startDateCalendar.get(Calendar.YEAR), startDateCalendar.get(Calendar.MONTH), startDateCalendar.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMinDate(startDateCalendar.getTimeInMillis());
// Create a new instance of DatePickerDialog and return itreturn datePickerDialog;
}
publicvoidonDateSet(DatePicker view, int year, int month, int day) {
// Do something with the date chosen by the user
view.setMinDate(fromDate);
todate.setText(day + "/" + (month + 1) + "/" + year); } }
Solution 4:
you can use setMinDate and setMaxDate methods to the datepickerdialog.
eg: DatePicker toPicker = Util.getDatePicker(getActivity(), toDateSetListener, toYear, toMonth, toDay) .getDatePicker(); toPicker.setMinDate(getToMinDate()); toPicker.setMaxDate(System.currentTimeMillis());
Solution 5:
So when you close your fromDate Dialog just store the variables in class level.
fromYear fromMonth fromDay
then when you launch toDate(fromYear, fromMonth, fromDay) to the dialog then instead of calendar. get current dates, just use the from and add +1 to the day for starting the picker at the next day. Make sense?
Post a Comment for "Android Show The Datepicker(end Date) Greater Than The Start Date?"