How to calculate the remaining days left for a persons next birthday from the date pickers selected date in android? -
i have written program calculate age of person according date selected date picker , , working fine. added method display days remaining , day on persons birthday falls on next birthday
private void calculatenextbirthday() { // todo auto-generated method stub date dt = new date(); simpledateformat sdf = new simpledateformat( "yyyy/mm/dd"); final string strbday = sdf.format(dt); try { dt = sdf.parse(strbday); } catch (final java.text.parseexception e) { // todo auto-generated catch block e.printstacktrace(); } final calendar c = calendar.getinstance(); c.settime(dt); // c.add(calendar.date, value); final calendar today = calendar.getinstance(); // take dob month , compare current // month final int bmonth = c.get(calendar.month); final int cmonth = today.get(calendar.month); c.set(calendar.year, today.get(calendar.year)); c.set(calendar.day_of_week, today.get(calendar.day_of_week)); if (bmonth <= cmonth) { c.set(calendar.year, today.get(calendar.year) + 1); } // result in millis final long millis = c.gettimeinmillis() - today.gettimeinmillis(); // convert days final long days = millis / 86400000; // precalculated // (24 * // 60 * // 60 * // 1000) // final string dayoftheweek = // sdf.format(bday.gettime()); sdf = new simpledateformat("eeee"); final string dayoftheweek = sdf.format(dt); txt8.settext("" + days + " days"); txt10.settext("" + dayoftheweek); }
the problem in program remaining days left works on basis of todays date not dates selected date picker, means if select other date date picker remaining days remains same , dayoftheweek shows same , want both change according dates selected
also attaching 2 screenshots 2 different birthdates , output same on last 2 rows of screenshot
my program shows age of person correctly when selected date date picker , in same way want remaining days , day on persons birthday falls change, when selected date date picker please need changes need in above code suggestions helpful thanking you
issue is:
you calculating difference using current date.
see little modified function:
private void calculatenextbirthday() { // todo auto-generated method stub /*date dt = new date(); //this current date, causing issue. should user selected date simpledateformat sdf = new simpledateformat( "yyyy/mm/dd"); final string strbday = sdf.format(dt); try { dt = sdf.parse(strbday); } catch (final java.text.parseexception e) { // todo auto-generated catch block e.printstacktrace(); }*/ final calendar c = calendar.getinstance(); c.set(1990, 8, 03); //this birthdate. set date date picker // c.add(calendar.date, value); final calendar today = calendar.getinstance(); // take dob month , compare current // month final int bmonth = c.get(calendar.month); final int cmonth = today.get(calendar.month); final int bdate = c.get(calendar.day_of_month); final int cdate = today.get(calendar.day_of_month); c.set(calendar.year, today.get(calendar.year)); c.set(calendar.day_of_week, today.get(calendar.day_of_week)); if (bmonth < cmonth) { c.set(calendar.year, today.get(calendar.year) + 1); } //added condition doesn't add in case birthdate greater current date . i.e. yet come in month. else if (bmonth == cmonth){ if(bdate < cdate){ c.set(calendar.year, today.get(calendar.year) + 1); } } // result in millis final long millis = c.gettimeinmillis() - today.gettimeinmillis(); // convert days final long days = millis / 86400000; // precalculated // (24 * // 60 * // 60 * // 1000) // final string dayoftheweek = // sdf.format(bday.gettime()); /*sdf = new simpledateformat("eeee"); final string dayoftheweek = sdf.format(dt);*/ toast.maketext(getapplicationcontext(), string.valueof(days), toast.length_short).show(); }
hope helps.
Comments
Post a Comment