如何将android联系人date-of birth-string转换为日期格式?

时间:2016-02-01 06:56:18

标签: android

在我的Android项目中,我想将联系人的出生日期从用户区域设置转换为字符串到日期

  String whereBday = ContactsContract.Data.MIMETYPE + "= ? AND " +
            ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
            ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;

     String[] paramsBday = new String[]{ 
            ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE   };

    Cursor bDayCursor = contentResolver.query(ContactsContract.Data.CONTENT_URI,
                    null,  whereBday, paramsBday, null); 

    while (bDayCursor.moveToNext()) { 

//date of birth as String format
        String bDay = bDayCursor.getString(bDayCursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
       //converting string to date format
       SimpleDateFormat formatter = new SimpleDateFormat(bDay);
        Date myDate= formatter.parse(mytime);
        DateFormat f = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.getDefault());
        String formattedDate = f.format(new Date());
        System.out.println("Date: " + formattedDate);

    }

     bDayCursor.close();

1 个答案:

答案 0 :(得分:2)

不幸的是,似乎没有关于日期格式存储的固定标准。最佳解决方案是遍历日期格式列表,并查看它是否与以下任何格式匹配。一旦你得到date对象,你可以修改它以使用你自己的格式化程序以任何格式打印。如果找不到,则可以检查不支持的格式,并将其添加到阵列中。

您可以使用以下代码:

public static final SimpleDateFormat[] birthdayFormats = {
        new SimpleDateFormat("yyyy-MM-dd"),
        new SimpleDateFormat("yyyy.MM.dd"),
        new SimpleDateFormat("yy-MM-dd"),
        new SimpleDateFormat("yy.MM.dd"),
        new SimpleDateFormat("yy/MM/dd"),
        new SimpleDateFormat("MMM dd, yyyy")
};

private void printBirthdays() {
    Cursor cursor = getContactsBirthdays();
    int bDayColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
    while (cursor.moveToNext()) {
        String bDay = cursor.getString(bDayColumn);
        Date birthday = null;
        boolean found = false;
        for (SimpleDateFormat f : birthdayFormats) {
            try {
                birthday = f.parse(bDay);
                Log.e(TAG, bDay + " = " + birthday.toString());
                found = true;
                break;
            } catch (ParseException e) {
            }
        }
        if (!found) {
            Log.e(TAG, bDay);
        }
    }
}

private Cursor getContactsBirthdays() {
    Uri uri = ContactsContract.Data.CONTENT_URI;

    String[] projection = new String[]{
            ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Event.CONTACT_ID,
            ContactsContract.CommonDataKinds.Event.START_DATE
    };

    String where =
            ContactsContract.Data.MIMETYPE + "= ? AND " +
                    ContactsContract.CommonDataKinds.Event.TYPE + "=" +
                    ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
    String[] selectionArgs = new String[]{
            ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
    };
    String sortOrder = null;
    return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}