Android CalendarProvider查询提供奇怪的开始和结束时间

时间:2015-02-01 18:40:34

标签: android google-calendar-api android-calendar

因此,当我向CalendarProvider查询个人日历的事件列表时,我得到了一些非常奇怪的开始和结束时间。例如,我在查询中看到的结果是1970-01-13,但在我的Google日历应用中,它显示为2015-02-01。我得到的所有事件都有这样的奇怪时期。

我的查询如下。

 String[] mProjection = new String[]{
            Events.CALENDAR_ID,                       
            Events.ORGANIZER,
            Events.TITLE,
            Events.DTSTART,
            Events.DTEND,
            Events._ID
    };

    ContentResolver cr = getActivity().getContentResolver();
    Uri uri = Events.CONTENT_URI;
    String selection = Events.CALENDAR_ID + " = ?";
    String[] selectionArgs = new String[] {"2"};
    Cursor cur = cr.query(uri, mProjection, selection, selectionArgs, null);

    Log.i(TAG, "events " + cur.getCount());

    TextView textView = (TextView) rootView.findViewById(R.id.dummy_text);
    while (cur.moveToNext()) {
        int calID = cur.getInt(0);
        String organizer = cur.getString(1);
        String title = cur.getString(2);
        int start = cur.getInt(3);
        int end = cur.getInt(4);
        int event_id = cur.getInt(5);

        Calendar startCal = Calendar.getInstance();
        startCal.setTimeInMillis(start);
        Calendar endCal = Calendar.getInstance();
        endCal.setTimeInMillis(end);

这是我将时间转换为可读的代码。

public  static String getHumanDate(int year, int month, int dayOfMonth) {
        String monthZero = "";
        String dayOfMonthZero = "";
        if ((month + 1) < 10)
            monthZero = "0";
        if (dayOfMonth < 10)
            dayOfMonthZero = "0";

        return year + "-" + monthZero + (month + 1) + "-" + dayOfMonthZero + dayOfMonth;
    }

1 个答案:

答案 0 :(得分:0)

想出来。应该用过

    long start = cur.getLong(3);
    long end = cur.getLong(4);

而不是int。