14

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 ?

android developer
  • 114,585
  • 152
  • 739
  • 1,270

1 Answers1

7

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.

Community
  • 1
  • 1
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Have you checked it? Does it really work well? Please try it out on multiple Android devices as I've already tried this format. – android developer Jul 20 '15 at 10:54
  • As I've written, this doesn't work. Why do you guys upvote it without checking it out? For example, on Nexus 4, when editing the contact, I can't edit the birthdate (nothing happens when I click on it). I need to remove it and add it again. – android developer Jul 26 '15 at 10:56
  • At first, sorry that I didn't responded earlier. I checked the documentation and also what others did. On which devices does it not work for you? You mentioned the Nexus 4 which ROM are talking about? The original one? Which version? For the Nexus devices the relevant code should be open source. Are you sure that this is not just a device specific bug? I know dozens of font color bugs on Gingerbread devices (just as example). – rekire Jul 26 '15 at 11:29
  • I've tested it on multiple devices: 2 Nexus 4 (one with kitkat and one with lollipop), LG G2 with 4.2 , HTC One M8 (Lollipop), Galaxy S4 (Lollipop), ... All stock . For all the solutions I've tried, I had issues with at least one of them. The one I've reported here is Nexus 4 with Kitkat. – android developer Jul 26 '15 at 17:59
  • How did it get +6 upvotes? All those people didn't test it? :( Anyway, I don't care about the points, but it's just that I still didn't get a solution for this... – android developer Jul 28 '15 at 12:59
  • I can confirm that this highlights the correct content and (almost) string formatting for adding an event using ContentProviderOperation. Tested on Pixel and Nexus 5 running 7.x. I changed the formatter to yyyy-MM-dd. – ninehundreds Sep 25 '17 at 23:28