Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying to build an Age Calculator
    text
    copied!<p>If I'm given a <strong>Date of Birth (d.o.b.)</strong> then how we <strong><em>Calculate current Age</em></strong> in android:</p> <pre><code> protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDateDisplay = (TextView) findViewById(R.id.dateDisplay); textView_cage = (TextView) findViewById(R.id.textView_cage); textView_currentagediff = (TextView) findViewById(R.id.textView_currentagediff); textView4 = (TextView) findViewById(R.id.textView4); mPickDate = (Button) findViewById(R.id.pickDate); mPickDate.setOnClickListener(new View.OnClickListener() { @SuppressLint("ValidFragment") @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onClick(View v) { DialogFragment newFragment = new DatePickerFragment(); newFragment.show(getFragmentManager(), "datePicker"); } }); } @TargetApi(Build.VERSION_CODES.HONEYCOMB) class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { public Dialog onCreateDialog(Bundle savedInstanceState) { final Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); int month = c.get(Calendar.MONTH); int day = c.get(Calendar.DAY_OF_MONTH); System.out.println("Current Setdate"); System.out.println("Year" + year); System.out.println("Month" + month); System.out.println("Date" + day); return new DatePickerDialog(getActivity(), this, year, month, day); } @Override public void onDateSet(DatePicker view, int year, int month, int day) { mDateDisplay.setText(String.valueOf(day) + "/" + String.valueOf(month + 1) + "/" + String.valueOf(year)); Date d = new Date(year, month, day); System.out.println("Current Setdate"); Date d1 = new Date(); int year2 = d1.getYear(); int month2 = d1.getMonth(); int day2 = d1.getDate(); System.out.println("Current aaj ki date"); System.out.println("Year" + year2); System.out.println("Month" + month2); System.out.println("Date" + day2); int year3 = year2 - year; int month3 = (month2 + 1) - month; int day3 = day2 - day; System.out.println("Current aaj kitnhesal k ho gaye "); System.out.println("Year" + year3); System.out.println("Month" + month3); System.out.println("Date" + day3); setnewage(year3, month3, day3); } private void setnewage(int year4, int month4, int day4) { Date d5 = new Date(year4, month4, day4); CharSequence day = DateFormat.format("d", d5); CharSequence month = DateFormat.format("M", d5); CharSequence year = DateFormat.format("yyyy", d5); textView_currentagediff.setText(year + " Years " + month + " Months and " + day + " days"); Date d1 = new Date(); int year2 = d1.getYear(); int month2 = d1.getMonth(); int day2 = d1.getDate(); } } } </code></pre> <p>I want to select the date of birth by <code>Datepickerdialog</code> and after selection of the date, I want to get Current Age. Please give me some example in Android.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload