android - Trying to build an Age Calculator -


if i'm given date of birth (d.o.b.) how calculate current age in android:

    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 , " + day + " days");             date d1 = new date();             int year2 = d1.getyear();             int month2 = d1.getmonth();             int day2 = d1.getdate();       }      }  } 

i want select date of birth datepickerdialog , after selection of date, want current age. please give me example in android.

you can try this:

// current date in format specified.     public string getcurrentdate(){      simpledateformat df = new simpledateformat("dd-mmmm-yyyy hh:mm aa");     return  df.format(calendar.getinstance().gettime()); }  public void getdatedifference(string dob){              string dateofbirth = dob;         string datecurrent = getcurrentdate();          //hh converts hour in 24 hours format (0-23), day calculation         simpledateformat format = new simpledateformat("mm/dd/yyyy hh:mm:ss");          date d1 = null;         date d2 = null;          try {             d1 = format.parse(dateofbirth);             d2 = format.parse(datecurrent);              //in milliseconds             long diff = d2.gettime() - d1.gettime();              long diffseconds = diff / 1000 % 60;             long diffminutes = diff / (60 * 1000) % 60;             long diffhours = diff / (60 * 60 * 1000) % 24;             long diffdays = diff / (24 * 60 * 60 * 1000);                         } catch (exception e) {             e.printstacktrace();         } } 

step 1 : date of birth entered using datepickerdialog . call method getdatedifference , pass value of dob argument.

step 2 : set difference obtained text view using settext() method.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -