-3

I am retrieving different formats of birthday dates from contacts and callender application.

From these dates i want to calculate number of days left for next birthday from current date.

Please help me how to do that?

TextView left = (TextView) view.findViewById(R.id.dayslefy);
String birthday = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
left.setText(getDaysLeft(birthday)+ " days left");
public static int getDaysLeft(String dobString) {
    Calendar dob = getCalendarFromString(getNextBirthday(dobString));
    Calendar now = Calendar.getInstance();

    now.set(Calendar.HOUR, dob.get(Calendar.HOUR));
    now.set(Calendar.MINUTE, dob.get(Calendar.MINUTE));
    now.set(Calendar.SECOND, dob.get(Calendar.SECOND));

    long dobSeconds = dob.getTimeInMillis() / MILLISECONDS_IN_SECOND;
    long nowSeconds = now.getTimeInMillis() / MILLISECONDS_IN_SECOND;

    long diff = dobSeconds - nowSeconds;

    return (int) (diff / SECONDS_IN_DAY);
}

/**
 * Deteremines the date of this persons next birthday.
 *
 * @param dobString
 * @return The next birthday
 */
public static String getNextBirthday(String dobString) {
    Calendar dob = getCalendarFromString(dobString);
    Calendar now = Calendar.getInstance();

    dob.set(Calendar.YEAR, now.get(Calendar.YEAR));

    if (!dob.before(now)) {
        StringBuffer sbuffer = new StringBuffer();

        int year = dob.get(Calendar.YEAR);
        int month = dob.get(Calendar.MONTH) + 1;
        int day = dob.get(Calendar.DAY_OF_MONTH);

        sbuffer.append(year);
        sbuffer.append('-');
        if (month < 10)
            sbuffer.append('0');
        sbuffer.append(month);
        sbuffer.append('-');
        if (day < 10)
            sbuffer.append('0');
        sbuffer.append(day);

        return sbuffer.toString();
    } else {
        StringBuffer sbuffer = new StringBuffer();

        int year = dob.get(Calendar.YEAR) + 1;
        int month = dob.get(Calendar.MONTH) + 1;
        int day = dob.get(Calendar.DAY_OF_MONTH);

        sbuffer.append(year);
        sbuffer.append('-');
        if (month < 10)
            sbuffer.append('0');
        sbuffer.append(month);
        sbuffer.append('-');
        if (day < 10)
            sbuffer.append('0');
        sbuffer.append(day);

        return sbuffer.toString();
    }
}

/**
 * Get a Calendar object from a date string (YYYY-MM-DD)
 * Processess a date string (YYYY-MM-DD) into a Calendar object and returns
 * to calling method.
 *
 * @param dateString The string to create the Calendar from
 * @return The Calendar object created from the date string given.
 */
public static Calendar getCalendarFromString(String dateString) {
    Calendar dateCal = Calendar.getInstance();
    dateCal.clear();
    dateCal.set(Integer.parseInt(dateString.substring(0, 3)), Integer.parseInt(dateString.substring(4, 7)), Integer.parseInt(dateString.substring(8, 10)));

    return dateCal;
}


    Process: com.techchai.reminder, PID: 3324
    java.lang.NumberFormatException: Invalid int: "Apr"
            at java.lang.Integer.invalidInt(Integer.java:138)
            at java.lang.Integer.parse(Integer.java:410)
            at java.lang.Integer.parseInt(Integer.java:367)
            at java.lang.Integer.parseInt(Integer.java:334)
            at com.techchai.reminder.activity.BirthdayFragment.getCalendarFromString(BirthdayFragment.java:293)
            at com.techchai.reminder.activity.BirthdayFragment.getNextBirthday(BirthdayFragment.java:238)
            at com.techchai.reminder.activity.BirthdayFragment.getDaysLeft(BirthdayFragment.java:216)
            at com.techchai.reminder.activity.BirthdayFragment$DataAdapter.bindView(BirthdayFragment.java:199)
            at android.widget.CursorAdapter.getView(CursorAdapter.java:254)
            at android.widget.AbsListView.obtainView(AbsListView.java:2823)
            at android.widget.ListView.makeAndAddView(ListView.java:1889)
            at android.widget.ListView.fillDown(ListView.java:713)
            at android.widget.ListView.fillFromTop(ListView.java:779)
            at android.widget.ListView.layoutChildren(ListView.java:1698)
            at android.widget.AbsListView.onLayout(AbsListView.java:2627)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:907)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
            at android.view.View.layout(View.java:16701)
            at android.view.ViewGroup.layout(ViewGroup.java:5328)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2344)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2057)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1216)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6678)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(L
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 1
    What have you tried and what was the result? As you did in school... please show your work. :) It's part of the process of getting questions answered on SO. It's helpful to you because it forces you to investigate your own problem and think it through. It also proves to readers that you did your homework and made a reasonable attempt to answer your own question. Thirdly, it helps readers find and diagnose the problem resulting in a better answer for you and less time wasted for us. – JeffC Sep 20 '15 at 02:55
  • Updated code. And error report – Chaitanya Reddy Sep 20 '15 at 03:07
  • Pretty clear from error stack that the Date string contains 'Apr', which cannot be parsed, and hence generates the error. – dbugger Sep 20 '15 at 12:44

1 Answers1

0

Simply use the Java Calendar class to get the next years date of birth per birthday.

Calendar calendar = Calendar.getInstance();
calendar.setTime(birthdayDate);
calendar.add(Calendar.YEAR, 1); // Add one year
Date nextYear = cal.getTime();
Kevin Crain
  • 1,905
  • 1
  • 19
  • 28
  • This works as, if my DOB is 05-10-1992 then it will return Next DOB as 05-10-1993. Plus one year with you DOB year. – Arnold Brown Sep 19 '19 at 11:09