我如何在 Android 中限制日历中的未来日期
How I restrict Future Date in Calender in Android
这是我的日历选择器代码。
我想用 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
请帮帮我
试试这个你可以使用 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);
下面的代码阻止了未来的 dates.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;
}
您必须像下面那样设置 属性 以限制最小和最大日期。
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();
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;
}
这是我的日历选择器代码。
我想用 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
请帮帮我
试试这个你可以使用 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);
下面的代码阻止了未来的 dates.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;
}
您必须像下面那样设置 属性 以限制最小和最大日期。
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();
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;
}