Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1037881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:54:16+00:00 2026-05-16T14:54:16+00:00

I have code to read contact details and to read birthdays. But how do

  • 0

I have code to read contact details and to read birthdays. But how do I get a list of contacts in order of their upcoming birthday?

For a single contact identified by id, I get details and birthday like this:

Cursor c = null;
  try {
   Uri uri = ContentUris.withAppendedId(
     ContactsContract.Contacts.CONTENT_URI, id);
   c = ctx.getContentResolver().query(uri, null, null, null, null);
   if (c != null) {
    if (c.moveToFirst()) {
     DatabaseUtils.cursorRowToContentValues(c, data);
    }

   }
   c.close();

   // read birthday
   c = ctx.getContentResolver()
     .query(
       Data.CONTENT_URI,
       new String[] { Event.DATA },
       Data.CONTACT_ID + "=" + id + " AND "
         + Data.MIMETYPE + "= '"
         + Event.CONTENT_ITEM_TYPE + "' AND "
         + Event.TYPE + "=" + Event.TYPE_BIRTHDAY,
       null, Data.DISPLAY_NAME);
   if (c != null) {
    try {
     if (c.moveToFirst()) {
      this.setBirthday(c.getString(0));
     }
    } finally {
     c.close();
    }
   }

   return super.load(id);
  } catch (Exception e) {
   Log.v(TAG(), e.getMessage(), e);
   e.printStackTrace();
   return false;
  } finally {
   if (c != null)
    c.close();
  }

and the code to read all contacts is:

public Cursor getList() {
        // Get the base URI for the People table in the Contacts content
        // provider.
        Uri contacts = ContactsContract.Contacts.CONTENT_URI;
        // Make the query.
        ContentResolver cr = ctx.getContentResolver();
        // Form an array specifying which columns to return.
        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME };

        Cursor managedCursor = cr.query(contacts, projection, null, null,
                ContactsContract.Contacts.DISPLAY_NAME
                        + " COLLATE LOCALIZED ASC");
        return managedCursor;
    }
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:54:17+00:00Added an answer on May 16, 2026 at 2:54 pm

    I did this the other way round – a selection directly to Data table which stores birthday.

    private static final int UPCOMING_COUNT = 10;
    
    
    public static List<BContact> upcomingBirthday(Context ctx) {
        String today = new SimpleDateFormat("MM-dd").format(new Date());        
        List<BContact> firstPart = upcomingBirthday(ctx, today, "12-31", UPCOMING_COUNT);
        if (firstPart.size() < UPCOMING_COUNT) {
            firstPart.addAll(upcomingBirthday(ctx, "01-01", today, UPCOMING_COUNT - firstPart.size()));
        }
        return firstPart;
    }
    
    
    public static List<BContact> upcomingBirthday(Context ctx, String fromDate, String toDate, int rows) {
    
      Uri dataUri = ContactsContract.Data.CONTENT_URI;
    
      String[] projection = new String[] { ContactsContract.Contacts.DISPLAY_NAME,                         
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE
                };
    
    
      Cursor c = ctx.getContentResolver().query(
           dataUri,
           projection, 
           ContactsContract.Data.MIMETYPE + "= ? AND " + 
           ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY + 
           " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) >= ?" + 
           " AND substr(" + ContactsContract.CommonDataKinds.Event.START_DATE + ",6) <= ?" ,
           new String[] {ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE, fromDate, toDate}, 
           "substr("+ ContactsContract.CommonDataKinds.Event.START_DATE +",6)");
      List<BContact> result = new ArrayList<BContact>();
      int i=0;
      while (c.moveToNext() && i<rows) {
          result.add(new BContact(c.getString(0), c.getString(1), c.getString(2)));
          i++;
      }
      c.close();
      return result;
    

    }

    The SELECT is called twice – one call from today to 12/31 and second call from 01/01 to today. I limit returned rows to 10 but this is not necesarry.

    EDIT: I found that this won’t work on all Android phones as birthday dates are stored in many different format. So you have to load all birthdays (unordered), parse it and sort it in memory. #Fail

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code to read contacts, get name and number and put them in
I have read my code again and again, but I can't figure out whats
I have a problem with my code. I need to get the contact photo
I have some code to read from a pdf file. Is there a way
I have made a code that read data from flash Nand (without filesystem). fd
I have written to code to read and write to registry which is working
I have used the code below to read rfid tag values. try { if
I have the code: $getCookieData = $this->Cookie->read('data'); $getUser = $this->User->find('first', array('conditions' => array('User.username' =>
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
I have the following code: $SQL = UPDATE jobs SET read = '1' WHERE

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.