日历中两个事件的不同时间

Different time of two events in a Calendar

我一直在实现一个应用程序,并且正在做一个带有函数的 DAO class,它将 return 当天的所有事件。但是,我有一个小错误。例如:我们有 2 个事件 - 事件 1(02:00-03:00) 和事件 2(14:00-16:00)。我们希望 event1 成为行中的第一个。好的,我们已经实现了一个排序,但是! event2 为 1516024800000 ms,event1 为 1542618000000.

我知道给你一个工作样本会很有帮助,但我不能...

这是这个函数:

public List<Schedule> getScheduleByDate(int year, int month, int day, String Account) {
    List<Schedule> schedules = new ArrayList<>();
    List<CalendarClass> calendarClasses = mCalendarClassDao.getTrueCalendars();
    /*if(Account.equals("ANONYMOUS")){
        return schedules;
    }*/
    String[] INSTANCE_PROJECTION = new String[]{
            CalendarContract.Instances.CALENDAR_ID,      // 0
            CalendarContract.Instances.TITLE,         // 1
            CalendarContract.Instances.DESCRIPTION,
            CalendarContract.Instances.DTSTART,
            CalendarContract.Instances.DTEND,
            CalendarContract.Instances.DISPLAY_COLOR,
            CalendarContract.Instances.EVENT_COLOR,
            CalendarContract.Instances.EVENT_COLOR_KEY,
            CalendarContract.Instances.ALL_DAY,
            CalendarContract.Instances.EVENT_LOCATION,
            CalendarContract.Instances.OWNER_ACCOUNT,
            CalendarContract.Instances.RRULE,
            CalendarContract.Instances.ORIGINAL_INSTANCE_TIME
    };
    Calendar startTime = Calendar.getInstance();
    startTime.set(year, month, day, 0, 0, 0);
    long time = startTime.getTimeInMillis();
    //time -= 1000;
    //end fix
    Calendar endTime = Calendar.getInstance();
    endTime.set(year, month, day, 23, 59, 59);
    long endMillis = endTime.getTimeInMillis();


    for (int i = 0; i < calendarClasses.size(); ++i) {
        //String selection = "(( " + CalendarContract.Events.DTSTART + " >= " + time + " ) AND ( " + CalendarContract.Events.DTSTART + " <= " + endTime.getTimeInMillis() + " ) AND ( " + CalendarContract.Events.CALENDAR_ID + " = " + "'" + calendarClasses.get(i).getId() + "'" + " ))";
        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.READ_CALENDAR}, 1000);
        }
        String selection = CalendarContract.Instances.CALENDAR_ID + " = " + "'" + calendarClasses.get(i).getId() + "'";
        //Cursor cursor = mContext.getContentResolver().query(CalendarContract.Events.CONTENT_URI, projection, selection, null, null);
        // sort
        Uri.Builder builder = CalendarContract.Instances.CONTENT_URI.buildUpon();
        ContentUris.appendId(builder, time);
        ContentUris.appendId(builder, endMillis);
        Cursor cursor = mContext.getContentResolver().query(builder.build(),
                INSTANCE_PROJECTION,
                selection,
                null,
                CalendarContract.Instances.DTSTART);


        while (cursor.moveToNext()){
            Log.wtf("1", "1");
            Schedule schedule = new Schedule();
            schedule.setTitle(cursor.getString(1));
            schedule.setDesc(cursor.getString(2));
            schedule.setTime(cursor.getLong(3));
            schedule.setTime_end(cursor.getLong(4));
            schedule.setColor(cursor.getInt(5));
            schedule.setLocation(cursor.getString(9));
            schedule.setAccount(cursor.getString(10));
            schedule.setRepeat(cursor.getString(11));
            schedules.add(schedule);
        }
        cursor.close();
    }
    Log.wtf("wtf", String.valueOf(schedules.size()));


    if (schedules.size() > 1) {
        int i = 0;
        int goodPairsCounter = 0;
        while (true) {
            long time1 = schedules.get(i).getTime();
            long time2 = schedules.get(i + 1).getTime();
            Log.wtf("TIME", String.valueOf(time1) + " - " + String.valueOf(time2));
            Log.wtf("TIME", schedules.get(i).getTitle() + " - " + schedules.get(i+1).getTitle());
            if (time1 > time2) {
                Log.wtf("TIME", "hop");
                Schedule sh = new Schedule(schedules.get(i).getId(), schedules.get(i).getColor(), schedules.get(i).getTitle(), schedules.get(i).getDesc(), schedules.get(i).getLocation(), schedules.get(i).getState(), schedules.get(i).getTime(), schedules.get(i).getTime_end(), schedules.get(i).getYear(), schedules.get(i).getRepeat(), schedules.get(i).getAccount());
                schedules.remove(i);
                schedules.add(i + 1, sh);
                goodPairsCounter = 0;
            } else {
                goodPairsCounter++;
            }
            i++;
            if (i == schedules.size() - 1) {
                i = 0;
            }
            if (goodPairsCounter == schedules.size() - 1) break;

        }
    }

    return schedules;
}

Here is a screenshot

需要知道如何解决这个问题

关闭。还没解决。如果你被卡住了,那就找另一种方法