这是我的日历选择器代码。
我想用Comapre服务器日期而不是系统日期修复Max Calender
ShowDatePicker我从图像按钮单击调用并且日期日历打开
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;
}
}
我是Android新手
请帮帮我
答案 0 :(得分:2)
下面的代码阻止了将来的date.using setMaxDate方法,并采用系统日期。
@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;
}
Toast的示例代码:
if(day>c.get(Calendar.DAY_OF_MONTH){
Toast.makeText(mContext, "select valid day", Toast.LENGTH_LONG).show();
return false;
}
答案 1 :(得分:1)
尝试此操作,您可以使用setMaxDate()
方法,该方法用于设置此DatePicker支持的最大日期,自1970年1月1日00:00:00起在getDefault()时区内以毫秒为单位。
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);
答案 2 :(得分:1)
您必须设置如下所示的属性以限制最短和最长日期。
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();
答案 3 :(得分:0)
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;
}