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

Ravi
  • 4,872
  • 8
  • 35
  • 46

3 Answers3

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

This call to Calendar.set(int year, int month, int date) is supposed to take actual values for the year, month and day. Instead, you are passing in the integer field identifiers; see the YEAR example. These values are to identify the field you want to update when you call one of the other methods such as Calendar.set(int field, int value).

I suspect you want to be passing in the values for today's date instead. You can get today's date simply with Calendar.getInstance().

Graham Borland
  • 60,055
  • 21
  • 138
  • 179
  • after your suggestion i tried with statically entering todays date and it returned the correct answer but when i use dobYear , dobMonth , dobDay it again gives the wrong answer and i use dobYear , dobMonth , dobDay to set the datepicker with current date which works fine. how to resolve this – Ravi May 17 '13 at 10:11
2

Use this code :

// 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;
Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
2

You just need to remove the line

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

The line

calOne = Calendar.getInstance();

sets the time to the current time anyway.

Your code also doesn't take account of leap years, so might give problems in certain cases.

HexAndBugs
  • 5,549
  • 2
  • 27
  • 36
  • thanks it worked . one more thing i am checking the output in logCat everytime the date is set the answer is shown twice – Ravi May 17 '13 at 10:21