I tried to get the Free Time slots from the Google calendar API using android studio.
MainActivity.java(Part of code)
This is i tried code for get the Free Time slots.
private List<String> getDataFromApi() throws IOException {
// List the next 10 events from the primary calendar.
DateTime startDate = new DateTime(System.currentTimeMillis());
DateTime endDate = new DateTime(System.currentTimeMillis()+24 * 60 * 60 * 1000);
org.joda.time.DateTime startDateJoda= new org.joda.time.DateTime(startDate);
org.joda.time.DateTime endDateJoda= startDateJoda.plusHours(24);
List<String> eventStrings = new ArrayList<String>();
List<String> tests = new ArrayList<String>();
DateTime rootStart = startDate;
DateTime rootEnd = endDate;
org.joda.time.DateTime rootStartJoda = new org.joda.time.DateTime(rootStart);
org.joda.time.DateTime rootEndJoda = new org.joda.time.DateTime(rootEnd);
Events events = mService.events().list("primary")
.setMaxResults(10)
.setTimeMin(startDate)
.setTimeMax(endDate)
.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();
org.joda.time.DateTime eventStartJoda= new org.joda.time.DateTime(eventStart);
DateTime eventEnd = event.getEnd().getDateTime();
org.joda.time.DateTime eventEndJoda= new org.joda.time.DateTime(eventEnd);
if (index == 0 && startDateJoda.isBefore(eventStartJoda) ) {
freeSlots.add( new MyEvent(startDateJoda,eventEndJoda) );
}
else if (index == 0) {
startDate = event.getEnd().getDateTime();
org.joda.time.DateTime startDatejoda=new org.joda.time.DateTime(startDate);
}
// else if (new org.joda.time.DateTime(events.get(index - 1).getEnd().getDateTime()).isBefore(eventStartJoda)==-1) {
// freeSlots.add(new MyEvent
// (new org.joda.time.DateTime(items.get(index - 1).getEnd().getDateTime()) ,eventStartJoda));
// }
if (items.size() == (index + 1) && new org.joda.time.DateTime(event.getEnd().getDateTime()).isBefore(endDateJoda)) {
freeSlots.add(new MyEvent(eventEndJoda, endDateJoda));
}
}
if (items.size() == 0) {
freeSlots.add(new MyEvent(startDateJoda,endDateJoda));
}
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();
org.joda.time.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() >= rootStartJoda.getHourOfDay() && temp.endDate.getHourOfDay() <= rootEndJoda.getHourOfDay()) {
hourSlots.add(new MyEvent(temp.startDate,temp.endDate));
temp = null;
}
}
freeHours--;
}
}
List<MyEvent> FreeList = new ArrayList<MyEvent>();
for (MyEvent events2: FreeList) {
org.joda.time.DateTime start = events2.startDate;
org.joda.time.DateTime end = events2.endDate;
if (start == null) {
start = events2.startDate;
end = events2.endDate;
}
eventStrings.add(
String.format("(%s) - (%s)", start,end));
}
return eventStrings;
}
MyEvent.java
This is the class i get the events.
public 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;
}
}
For this code i get this Error.
No instant converter found for type: com.google.api.client.util.DateTime
What is the reason for that?
Glad if anyone can help
Thank You