Reputation: 69
This is My calender Picker Code.
I want to fix Max Calender with Comapre Server Date not System Date
ShowDatePicker I call from Image button click and Date Calendar is open
private void showDatePickerDialog(View v) {
dateFragment = new DatePickerFragment1();
dateFragment.show(getFragmentManager(), "datePicker");
}
public static class DatePickerFragment1 extends DialogFragment implements DatePickerDialog.OnDateSetListener {
private Context context;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
context=getActivity();
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
pYear = year;
pMonth = month;
pDay = dayOfMonth;
Creation_Date_Display();
}
private void Creation_Date_Display() {
StringBuffer DATE = (new StringBuffer().append(pYear).append("-")
.append(pMonth + 1).append("-")
.append(pDay).append(" "));
// CreationDate.setText(DATE);
// Use to make an Correct Format Of a Date
String input = String.valueOf(DATE);
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = df1.parse(input);
}catch (Exception e){
e.printStackTrace(e);
}
String formattedDateOrder = df1.format(date);
CreationDate.setText(formattedDateOrder);
Creation_Date = formattedDateOrder;
}
}
I am new in Android
Please Help me
Upvotes: 0
Views: 367
Reputation: 69
Date selectedDate = Date.valueOf(pYear+"-"+(pMonth+1)+"-"+pDay);
if(selectedDate.getTime()>System.currentTimeMillis()){
Toast.makeText(context, "Can not Select Future Date", Toast.LENGTH_SHORT).show();
selectedDate = new Date(System.currentTimeMillis());
/* pYear = selectedDate.getYear()+1900;
pMonth = selectedDate.getMonth();
pDay = selectedDate.getDay();*/
pYear = yearCheck;
pMonth = monthCheck;
pDay = dayCheck;
}
Upvotes: 0
Reputation: 491
below code prevent the future dates.using setMaxDate method,and takes system date.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
DatePickerDialog mDatePicker=new DatePickerDialog(getActivity(), this, year, month, day);
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
context=getActivity();
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
// Create a new instance of DatePickerDialog and return it
return mDatePicker;
}
sample code for Toast:
if(day>c.get(Calendar.DAY_OF_MONTH){
Toast.makeText(mContext, "select valid day", Toast.LENGTH_LONG).show();
return false;
}
Upvotes: 2
Reputation: 69734
try this you can use setMaxDate()
method which is used to Sets the maximal date supported by this DatePicker in milliseconds since January 1, 1970 00:00:00 in getDefault() time zone.
setMaxDate
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH)
DatePickerDialog mDatePicker = new DatePickerDialog(SearchResultClass.this,
R.style.MyDialogTheme,
new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
}
}, mYear, mMonth, mDay);
mDatePicker.getDatePicker().setMaxDate(System.currentTimeMillis());
mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
Upvotes: 1
Reputation: 6632
you have to set property like below to restrict minimum and maximum date.
DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, R.style.DatePicker, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
// to set Max Date
datePickerDialog.getDatePicker().setMaxDate(System.currentTimeMillis());
// to set Min Date
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
Upvotes: 1