如何使用 Android Studio 获取当前日期的 Google 日历免费时段列表?

How can I take the Google Calendar free slots list for the current date using Android Studio?

我从 Android 开发人员教程中获取 Event 列表,如下所示:

  private List<String> getDataFromApi() throws IOException {
        // List the next 10 events from the primary calendar.
        DateTime now = new DateTime(System.currentTimeMillis());
        List<String> eventStrings = new ArrayList<String>();
        List<String> tests = new ArrayList<String>();

        Events events = mService.events().list("primary")
                .setMaxResults(10)
                .setTimeMin(now)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();


        List<Event> items = events.getItems();

        for (Event event : items) {
            DateTime start = event.getStart().getDateTime();

            if (start == null) {
                // All-day events don't have start times, so just use
                // the start date.
                start = event.getStart().getDate();
            }
            eventStrings.add(
                    String.format("%s (%s) test", event.getSummary(), start));
        }

        return eventStrings;
    }

另外,下面的post也有类似的问题:

.

但我仍然不知道如何将该答案转换为 Android Studio (Java)。

请指导我如何操作 - 谢谢。

考虑到 DateTime 是来自 Joda 库的 org.joda.time.DateTime,它大致可以翻译为 -

N.B:日期时间比较取决于您使用的 class,因此您很可能需要更改这些。不要盲目复制粘贴

class MyEvent implements  Comparable<MyEvent>{
        public DateTime startDate; // org.joda.time.DateTime
        public DateTime endDate; // org.joda.time.DateTime

        public MyEvent(DateTime startDate, DateTime endDate) {
            this.startDate = startDate;
            this.endDate = endDate;
        }
        /**
         *
         * @param  anotherEvent
         * @return 0 for same time, 1 : this>anotherEvent, -1 : this<anotherEvent
         */
        @Override
        public int compareTo(@NonNull MyEvent anotherEvent) {
            // implement 0/1/-1 based on your DateTime object

            return 0;
        }
    }


    private void calender(){

        // List the next 10 events from the primary calendar.
        DateTime startDate = new DateTime(System.currentTimeMillis()); // org.joda.time.DateTime
        DateTime endDate  = startDate.plusHours(24); //endData 24 hours from start date

        DateTime rootStart = startDate;
        DateTime rootEnd = endDate;
        List<String> eventStrings = new ArrayList<String>();
        List<String> tests = new ArrayList<String>();

        Events events = mService.events().list("primary")
                .setMaxResults(10)
                .setTimeMax(endDate)
                .setTimeMin(startDate)
                .setOrderBy("startTime")
                .setSingleEvents(true)
                .execute();


        List<Event> items = events.getItems();

        int interval = 2 ; // how big single slot should be (in this case 2 hrs)
        ArrayList<MyEvent> freeSlots = new ArrayList<MyEvent>();
        for (int index =0;index<items.size();index++) {
            Event event = items.get(index);
            DateTime eventStart = event.getStart().getDateTime(); // TODO: create org.joda.time.DateTime obj using the return value of getDateTime()
            DateTime eventEnd = event.getEnd().getDateTime(); // TODO: create org.joda.time.DateTime obj using the return value of getDateTime()

            if (index == 0 && startDate.isBefore(eventStart) ) { 
                freeSlots.add( new MyEvent(startDate,eventStart) );
            }
            else if (index == 0) {
                startDate = event.getEnd().getDateTime(); // TODO: create org.joda.time.DateTime obj using the return value of getDateTime()
            }
            else if (events.get(index - 1).getEnd().getDateTime() /* // TODO: create org.joda.time.DateTime obj using the return value of getDateTime() */.isBefore(eventStart)==-1) {
                freeSlots.add(new MyEvent(items.get(index - 1).getEnd().getDateTime() /* // TODO: create org.joda.time.DateTime obj using the return value of getDateTime() */,eventStart));
            }

            if (items.size() == (index + 1) && event.getEnd().getDateTime().isBefore(endDate)) {
                freeSlots.add(new MyEvent(eventEnd, endDate));
            }
        }
        if (items.size() == 0) {
            freeSlots.add(new MyEvent(startDate,endDate));
        }

        ArrayList<MyEvent> hourSlots = new ArrayList<MyEvent>();
        MyEvent temp = null;
        for(int index =0;index<freeSlots.size();index++){
            MyEvent free = (MyEvent) freeSlots.get(index);

            int freeHours = free.endDate.getHourOfDay()- free.startDate.getHourOfDay();
            DateTime freeStart = free.startDate, freeEnd = free.endDate;
            while(freeStart.getHourOfDay()+freeHours+interval>=0) { // 11 + 4 + 2 >= 0
                if(freeHours>=interval) {
                    temp.startDate = free.startDate;
                    temp.endDate= temp.endDate.hourOfDay().setCopy(temp.endDate.getHourOfDay()+freeHours);
                    temp.startDate = free.startDate;
                    temp.startDate = temp.startDate.hourOfDay().setCopy(temp.startDate.getHourOfDay()+freeHours-interval);
                    if(temp.startDate.getHourOfDay() >= rootStart.getHourOfDay() && temp.endDate.getHourOfDay() <= rootEnd.getHourOfDay()) {
                        hourSlots.add(new MyEvent(temp.startDate,temp.endDate));
                        temp = null;
                    }
                }
                freeHours--;
            }
        }
    }

在你的 build.gradle 添加 -

implementation 'joda-time:joda-time:2.10'