日期之间的差异

时间:2013-05-17 09:54:31

标签: android android-datepicker

我正在使用datepicker来允许用户选择他的出生日期和onDateSet()方法我正在计算他的年龄。我看过很多人推荐这个,但它给出了错误的答案。

这是我的代码

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

我做错了什么

3 个答案:

答案 0 :(得分: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()即可获得今天的日期。

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

答案 2 :(得分:2)

您只需删除该行

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

该行

calOne = Calendar.getInstance();
无论如何,

将时间设置为当前时间。

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