0

I am using datepicker to allow the user to pick his date of birth and onDateSet() method i am calculating his age. I have read lot of people recommending this but it is giving wrong answer.

Here is my code

class DOBDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener{
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        // Use the current date as the default date in the picker
        return new DatePickerDialog(BookingFormActivity.this, this, dobYear, dobMonth, dobDay );
    }
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        // Do something with the date chosen by the user
        calOne = Calendar.getInstance();
        calTwo = Calendar.getInstance();

        calOne.set(Calendar.YEAR,Calendar.MONTH, Calendar.DAY_OF_MONTH);
        calTwo.set(year, monthOfYear, dayOfMonth);

          long milliseconds1 = calOne.getTimeInMillis();
          long milliseconds2 = calTwo.getTimeInMillis();

          long diff = milliseconds2 - milliseconds1;
          long sec = diff / 1000 ; 
          long minutes = sec / 60 ;
          long hr = minutes / 60 ;
          long days = hr / 24 ;
          long diffYears = days / 365 ; 
          //long diffYears = (diff / (24 * 60 * 60 * 1000 * 356));  
          Log.d("date", String.valueOf(diffYears));


        dobYear = year;
        dobMonth = monthOfYear;
        dobDay = dayOfMonth;
        updateDOBDateDisplay();
    }
}   

what am i doing wrong

4

3 回答 3

3
calOne.set(Calendar.YEAR,Calendar.MONTH, Calendar.DAY_OF_MONTH);

此调用Calendar.set(int year, int month, int date)应该采用年、月和日的实际值。相反,您传递的是整数字段标识符;见YEAR例子。当您调用其他方法之一时,这些值用于标识要更新的字段,例如Calendar.set(int field, int value).

我怀疑你想传递今天日期的值。您可以简单地使用 获取今天的日期Calendar.getInstance()

于 2013-05-17T09:59:35.283 回答
2

使用此代码:

// Calender Button onClickListener 
calender.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                  showDialog(DATE_DIALOG_ID);
            }
        });

    // ----------To Open datePicker----------------
    private DatePickerDialog.OnDateSetListener pDateSetListener = new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear,
                int dayOfMonth) {
            pYear = year;
            pMonth = monthOfYear;
            pDay = dayOfMonth;          
        }
    };

    // To create date dialog
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DATE_DIALOG_ID:
            return new DatePickerDialog(this, pDateSetListener, pYear, pMonth,
                    pDay);
        }
        return null;
    }

    // Find out age in year.
    final Calendar cal = Calendar.getInstance();
    int currentYear = cal.get(Calendar.YEAR);
    int nowAge = currentYear - pYear;
于 2013-05-17T10:06:17.500 回答
2

你只需要删除线

calOne.set(Calendar.YEAR,Calendar.MONTH, Calendar.DAY_OF_MONTH);

线

calOne = Calendar.getInstance();

无论如何将时间设置为当前时间。

您的代码也没有考虑闰年,因此在某些情况下可能会出现问题。

于 2013-05-17T10:06:55.980 回答