在Android中验证出生日期字段

时间:2013-07-08 07:57:51

标签: android validation datepicker android-sqlite android-datepicker

在我的应用程序中,我有一个出生日期字段,我需要验证...验证条件是用户不应该选择当前日期,如果选择它会显示错误消息... 以这种方式从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 个答案:

答案 0 :(得分:2)

粘贴下面的功能并替换您的对话框ID,按钮ID,并编辑出生日期选择器日期最简单的文本ID。您可以使用普通日期选择器,如果您只想删除vaidation以用于将来作为普通日期选择器的日期

    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));


}

答案 1 :(得分: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));
}

答案 2 :(得分: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;
        }

答案 3 :(得分: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