I am querying the ContactsContract.Data.CONTENT_URI with the following arguments, in order to get all of the device contacts' birthdays
final static Uri CONTENT_URI =
ContactsContract.Data.CONTENT_URI;
String SELECTION_ARGS = new String[] {
ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY)
};
String SELECTION =
ContactsContract.Data.MIMETYPE + " = ? AND "
+ ContactsContract.CommonDataKinds.Event.TYPE + " = ? AND "
+ ContactsContract.CommonDataKinds.Event.START_DATE + " NOT NULL";
String[] PROJECTION = {
Data._ID,
Data.CONTACT_ID,
Data.LOOKUP_KEY,
Utils.hasHoneycomb() ? Data.DISPLAY_NAME_PRIMARY : Data.DISPLAY_NAME,
Utils.hasHoneycomb() ? Data.PHOTO_THUMBNAIL_URI : Data.CONTACT_ID,
ContactsContract.CommonDataKinds.Event.START_DATE,
SORT_ORDER,
};
cur = getContext().getContentResolver().query(CONTENT_URI ,
ContactsQuery.PROJECTION,
SELECTION,
SELECTION_ARGS,
SORT_ORDER);
problem is that the START_DATE field might have different formatting in some cases. Some of the values returned are:
1990-08-11
--08-13
Jan 1, 1970
For some reason, all contacts fetched from Skype seems to be have their birthdays set to Jan 1, 1970.
Currently, I am parsing the dates like this:
private final static SimpleDateFormat yearFull = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
private final static SimpleDateFormat yearLess = new SimpleDateFormat("--MM-dd", Locale.US);
Calendar signCal = Calendar.getInstance();
Date date = null;
try {
date = yearFull.parse(birthday);
} catch (ParseException e) {
// e.printStackTrace();
Log.w(TAG, "No year in " + birthday);
// throw new IllegalArgumentException("Error parsing " + birthday);
try {
date = yearLess.parse(birthday);
} catch (ParseException e1) {
Log.e(TAG, "Couldn't parse yearLess " + birthday);
e1.printStackTrace();
}
}
signCal.setTime(date);
but it misses the Jan 1, 1970 case.
How many different formats are there? What is the best practice to use the START_DATE value of the Contacts table?
Is it possible to have a piece of code that handles all formats?