我正在尝试验证日历日期,但在验证时,无论输入如何,代码都会返回 false。
String strDateOfBirth = userInputArrayList.get(7) + "/" + userInputArrayList.get(8) + "/" + userInputArrayList.get(9);
Log.d(TAG, "1Date of Birth: " + strDateOfBirth);
SimpleDateFormat sdf = new SimpleDateFormat("dd/mm/yyyy");
Date dtDateOfBirth = parseDate(strDateOfBirth, sdf);
//Convert to epoch millis
long dobmillis = dtDateOfBirth.getTime();
Log.d(TAG, "date of birth in milli: " + dobmillis);
//Current time in epoch millis
long currmillis = Calendar.getInstance().getTimeInMillis();
Log.d(TAG, "current date in milli: " + currmillis);
Log.d(TAG, "less than: " + (19L * 365L * 24L * 60L * 60L * 1000L));
Log.d(TAG, "less than: " + (11L * 365L * 24L * 60L * 60L * 1000L));
if ((currmillis - dobmillis) > (19L * 365L * 24L * 60L * 60L * 1000L)) {
//More than 19 years
txtCalenderErrorCreateAccountPage.setText("must be 19 years of age");
nextPage = false;
} else if ((currmillis - dobmillis) < (11L * 365L * 24L * 60L * 60L * 1000L)) {
//less than 11 years
txtCalenderErrorCreateAccountPage.setText("must be 11 years of age");
nextPage = false;
} else {
txtCalenderErrorCreateAccountPage.setText("correct");
}
public Date parseDate(String date, SimpleDateFormat sdf) {
try {
Date d = sdf.parse(date);
return d;
} catch (ParseException e) {
showToast("There has been an error");
return null;
}
}
当出生日期意味着用户将在 11 到 19 岁之间时,我希望日期返回 true。
任何帮助将不胜感激!