如何根据DatePickerDialog android中的选择日期计算年龄?

时间:2015-11-10 13:44:25

标签: java android

我怎么能这样做?

这是我的代码:

private void setDateTimeField () {
    Calendar newCalendar = Calendar.getInstance();
    mDatePickerDialog = new DatePickerDialog(AddBirthday.this, new OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            Calendar newDate = Calendar.getInstance();
            newDate.set(year, monthOfYear, dayOfMonth);
            addBirthdayDate.setText(dateFormatter.format(newDate.getTime()));
            dateSelected = String.valueOf(dayOfMonth) + " /" + String.valueOf(monthOfYear + 1)
                    + " /" + String.valueOf(year);
        }
    }, newCalendar.get(Calendar.YEAR), newCalendar.get(Calendar.MONTH), newCalendar.get(Calendar.DAY_OF_MONTH));
}

我想现在计算用户在DatePickerDialog中选择日期并将其存储为整数类型时的生日。

2 个答案:

答案 0 :(得分:1)

您可以尝试使用更简单的代码,例如:

    private int mDay;
    private int mMonth;
    private int mYear;
    private EditText date;
    private Calendar c = new Calendar();

    ...

    //and in your onCreate method:
    date = (EditText)findViewById(R.id.date_of_birth_as_text);

    date.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
             showDialog(DATE_DIALOG_ID); //DATE_DIALOG_ID is the id of your DatePickerDialog - declared in your layout file

        }
    });

    //default values of the year, month and date - will be changed after click on the EditText view
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    updateDisplay();

    protected Dialog onCreateDialog(int id) {
        switch(id) {
            case DATE_DIALOG_ID:
            return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay); 
        }
        return null;
     }

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

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
            // TODO Auto-generated method stub
            c.set(Calendar.YEAR, year);
            c.set(Calendar.MONTH, monthOfYear);
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            mYear = c.get(Calendar.YEAR);
            mMonth = c.get(Calendar.MONTH);
            mDay = c.get(Calendar.DAY_OF_MONTH);

            updateDisplay();
        }
    };

private void updateDisplay() {
    Date currentDate = new Date();
    int age = currentDate.getYear() - mYear;
    date.setText( new StringBuilder().append("The user is ")
            .append(age).append(" years old"));
}

答案 1 :(得分:-1)

int age = getCurrentYear() - datePicker.getYear();