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
Simply use the Java
Calendar
class to get the next years date of birth per birthday.