我的Android应用程序需要一个日历,其日期为18年前的maxDate。到目前为止,我尝试了这段代码,但它只是说Integer number is too large
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
view.setMaxDate(System.currentTimeMillis() - 31556926000);
year_x = year;
month_x = monthOfYear + 1;
day_x = dayOfMonth;
etBirthDate.setText(month_x + "/" + day_x + "/" + year_x);
}
};
答案 0 :(得分:5)
要更好地了解您的问题,请参阅:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html部分整数文字。 它声明:
如果整数文字以字母L或l结尾,则其长度为long;否则它是int类型。建议您使用大写字母L,因为小写字母l很难与数字1区分开来。
最简单的解决方案:
view.setMaxDate(System.currentTimeMillis() - 31556926000L);
但我会选择:
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.YEAR, -18);
view.setMaxDate(calendar.getTimeInMillis());
答案 1 :(得分:-1)
final Calendar mcurrentDate = Calendar.getInstance();
int mYear = mcurrentDate.get(Calendar.YEAR);
int mMonth = mcurrentDate.get(Calendar.MONTH);
int mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(
getContext(), new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker,
int selectedyear, int selectedmonth,
int selectedday) {
selectedyear=selectedyear;
mcurrentDate.set(Calendar.YEAR, selectedyear);
mcurrentDate.set(Calendar.MONTH, selectedmonth);
mcurrentDate.set(Calendar.DAY_OF_MONTH, selectedday);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy", Locale.US);
edtxtDOB.setText(sdf.format(mcurrentDate.getTime()));
}
}, mYear-18, mMonth, mDay);
mcurrentDate.set(mYear-18,mMonth,mDay);
long value=mcurrentDate.getTimeInMillis();
mDatePicker.getDatePicker().setMaxDate(value);
mDatePicker.show();