-1

在我的应用程序中,我有一个需要验证的出生日期字段...验证条件是用户不应选择当前日期,如果选择它会显示错误消息...日期是DatePickerDialog从这边走:

private void updateDisplay() {
    tv_dob.setText(new StringBuilder()
    // Month is 0 based so add 1
            .append(mMonth + 1).append("-").append(mDay).append("-")

            .append(mYear - 1).append(" "));
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;
        updateDisplay();
    }
};


final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DAY_OF_MONTH);

    // set current date into textview
    tv_applndate.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(month + 1).append("-").append(day).append("-")
            .append(year));

如何验证此日期字段?

4

4 回答 4

2

粘贴下面的函数并替换您的对话框 id、按钮 id 和编辑文本 id 最简单的方法,用于生日日期选择器。如果您只想删除未来日期的验证,您可以使用普通日期选择器,它将作为普通日期选择器工作

    private void setDateTimeField() {

    et_dob.setOnClickListener(this);
    dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US); //dd/MM/yyyy

    Calendar newCalendar = Calendar.getInstance();
    dobDatePickerDialog = new DatePickerDialog(getActivity(), new OnDateSetListener() {

        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);

            Date current = newDate.getTime();
             int diff1 =new Date().compareTo(current);

                if(diff1<0){
                    Toast.makeText(getActivity(), "Please select a valid date",  Toast.LENGTH_LONG).show();
                    return;
                }
                else{
                    et_dob.setText(dateFormatter.format(newDate.getTime()));
                }

        }

    },newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));


}
于 2014-08-06T13:18:44.587 回答
0

你可以试试这个

et_dob.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }

    });
    final Calendar calendar = Calendar.getInstance();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    if (et_dob.getText().toString() != null) {
        try {
            calendar.setTime(df.parse(et_dob.getText().toString()));
        } catch (java.text.ParseException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        month = month_date.format(calendar.getTime());
    } else {
        mYear = calendar.get(Calendar.YEAR);
        mMonth = calendar.get(Calendar.MONTH);
        mDay = calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat month_date = new SimpleDateFormat("MMM");
        month = month_date.format(calendar.getTime());
    }

    if (cal_currentTime.compareTo(calendar) > 0)
        updateDisplay();
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;

        String dateSetter = (new StringBuilder().append(mYear).append("-")
                .append(mMonth + 1).append("-").append(mDay).append(""))
                .toString();
        final Calendar cal = Calendar.getInstance();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        if (dateSetter != null) {
            try {
                cal.setTime(df.parse(dateSetter));
            } catch (java.text.ParseException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            SimpleDateFormat month_date = new SimpleDateFormat("MMM");
            month = month_date.format(cal.getTime());
        }

        if (cal_currentTime.compareTo(cal) > 0)
            updateDisplay();
        else
            Toast.makeText(context, "Choose Proper date format",
                    Toast.LENGTH_SHORT).show();
    }
};

private void updateDisplay() {
    dateOfBirth = (new StringBuilder()
            // Month is 0 based so add 1
            .append(mYear).append("-").append(mMonth + 1).append("-")
            .append(mDay).append("")).toString();
    et_dob.setText(new StringBuilder()
            // Month is 0 based so add 1
            .append(mDay).append("-").append(month).append("-")
            .append(mYear));
}
于 2013-07-08T08:07:36.907 回答
0

在 ondateset compareTo 方法中使用此验证如果更大则返回 1,如果更小则返回 -1

Calendar cal = Calendar.getInstance();

        cal.set(year, month, day);
        Date current = cal.getTime();

        int diff1 =new Date().compareTo(current);

        if(diff1>0){
            Toast.makeText(ctx, "Please select a valid date.",  Toast.LENGTH_LONG).show();
            return;
        }
于 2013-07-08T08:09:23.073 回答
0

您所要做的就是将所选日期与您的OnDateSetListener.

    Calendar selectedCal = Calendar.getInstance();
    selectedCal.set(selectedYear, selectedMonth, selectedDay);
    Date selectedDate = selectedCal.getTime();
    Date currentDate = Calendar.getInstance().getTime();
    int diff1 = selectedDate.compareTo(currentDate);
    if(diff1>0){
        Toast.makeText(this,"Please select a valid date",  Toast.LENGTH_LONG).show();
        return;
    }
  // otherwise your date is valid  
于 2019-09-29T13:14:56.150 回答