我从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;
}
此外,下面的帖子也有类似的问题:
谷歌日历API:获取指定日期的免费老虎机列表。
但我一直纠结于如何将答案转换为Android Studio(Java(。
请指导我如何做到这一点——谢谢。
考虑到DateTime
是Joda库中的org.joda.time.DateTime
,它可以大致翻译为这个-
注:DateTime比较将取决于您使用的类,所以您很可能需要更改这些。不要盲目复制粘贴
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'