Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the correct way to put a birthdate into a contact data?

Background

I'm trying to update contacts data and put there the birthdate of each.

The problem

It seems that for each device I try (and I didn't even try that many devices), the insertion has one or more of those issues:

  • when viewing/editing the contact, the brithdate can't be clicked and edited.
  • when viewing/editing the contact, the format that's shown isn't the same as the one that's shown when the user puts the birthdate
  • Missing year, or totally wrong year.
  • when viewing the contact, the birthdate isn't shown, yet when editing it, it is shown. Also, the opposite.

What I've tried

I've tried using a timestamp and a full ISO8601 format (because of this link, meaning it's "yyyy-MM-dd HH:mm:ss"). I've also tried "yyyy-MM-dd" and I tried using the default format of the date of the device.

All had the mentioned issues (at least one for each).

Here's a piece of the code:

final Date birthdate = ...
// String birthdateStr = new SimpleDateFormat("yyyy-MM-dd").format(birthdate);
// String birthdateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(birthdate);
// String birthdateStr = new SimpleDateFormat(((SimpleDateFormat) java.text.DateFormat.getDateInstance(java.text.DateFormat.DEFAULT, Locale.getDefault())).toLocalizedPattern(),Locale.getDefault()).format(birthdate);


String birthdateStr = Long.toString(birthdate.getTime()/1000);
final Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
builder.withValue(Data.RAW_CONTACT_ID, ...)
       .withValue(ContactsContract.Data.MIMETYPE, Event.CONTENT_ITEM_TYPE)
       .withValue(ContactsContract.CommonDataKinds.Event.START_DATE, birthdateStr)
       .withValue(ContactsContract.CommonDataKinds.Event.TYPE,
                    ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY);

Of course, I've also looked on this issue here, and found similar issues, but none of the proposed solutions seem to work well.

The question

How should I really insert a birthdate into the contacts? How come each device has its own way to interpret the dates? What's the correct standard to put the birthdate ?

like image 907
android developer Avatar asked Jul 13 '15 11:07

android developer


People also ask

How do I add a birthday to a contact on Android?

Open the Google Contacts app on your smartphone. Now, search for the contact in which you want to add a birthday. Click on the Edit button (Pencil icon) at the top right corner. Now, tap the 'More fields' option at the bottom and look for the birthday option.

How do I change my birthday in contacts?

The standard Android phone app date field can be used for birthdays. So, go into your phone app, the one with the blue phone icon, select the contacts tab on top. Then select a contact. Then select the edit icon (a pencil.


1 Answers

Based on this answer, I would say that you should use the format YYYY-MM-DD, but you need to define a account type and account name. So add those lines:

.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)

Let me know if this helps.

like image 61
rekire Avatar answered Oct 07 '22 14:10

rekire