在我的Android应用程序中,我有2个字段,一个用于dateOfBirth,另一个用于dateOfMarriage。我想继续验证2个字段之间的差异必须是> 18年。有人可以帮忙吗?在此先感谢
答案 0 :(得分:1)
String dateStart = dob.getText().toString().trim();
String dateStop = dom.getText().toString().trim();
//Date format
SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = null;
Date d2 = null;
try {
d1 = format.parse(dateStart);
d2 = format.parse(dateStop);
} catch (ParseException e) {
e.printStackTrace();
}
long diff = d2.getYear() - d1.getYear();
long year = diff/365;
if (diff < 19){
System.err.println("Difference in number of years : " + diff);
Toast.makeText(DIffdates.this, "DOB & marg should have atleast 18 years gap ", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:-1)
mycalendar=Calendar.getInstance();
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd",Locale.US);
Date parse = null;
Date current =null;
Calendar calendar=Calendar.getInstance();
int current_year=calendar.get(Calendar.YEAR);
int dates=calendar.get(Calendar.DAY_OF_MONTH);
int hours=calendar.get(Calendar.HOUR_OF_DAY);
int minute=calendar.get(Calendar.MINUTE);
int months=calendar.get(Calendar.MONTH)+1;
int seconds=calendar.get(Calendar.SECOND);
TimeZone tz = TimeZone.getDefault();
String timezone = tz.getDisplayName(false, TimeZone.SHORT);
String current_date=(""+current_year+"/"+months+"/"+dates+timezone);
String month=mycalendar.getDisplayName(Calendar.MONTH, Calendar.SHORT, Locale.getDefault());
String day=mycalendar.getDisplayName(Calendar.DAY_OF_MONTH, Calendar.SHORT, Locale.getDefault());
mycalendar.set(Calendar.YEAR, year);
mycalendar.set(Calendar.MONTH, monthOfYear);
mycalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String selected_date=(""+year+"/"+monthOfYear+"/"+dayOfMonth+timezone);
try {
current=sdf.parse(current_date);
parse=sdf.parse(selected_date);
} catch (ParseException e) {
e.printStackTrace();
}
long diff=current_year-year;
if(diff==18){
if(dates>=dayOfMonth){
edit_login_date.setText(sdf.format(mycalendar.getTime()));
date_of_birth = edit_login_date.getText().toString();
}
else {
Toast.makeText(AddCandidates.this, "Age should be at least 18 years ", Toast.LENGTH_SHORT).show();
}
}
else if(diff>18){
edit_login_date.setText(sdf.format(mycalendar.getTime()));
date_of_birth = edit_login_date.getText().toString();
}
else {
Toast.makeText(AddCandidates.this, "Age should be at least 18 years ", Toast.LENGTH_SHORT).show();
}