Вопрос

I am making an android app that shows the Calendar day view, it looks like a list with colored blocks floating on top of it which represent event start time and end time.

I was able to do everything correctly, however I wanted to sort events based on the same times or overlapping times or events which have start time or end time within the range of other event's start time and end time.

Start time and end time are in Milliseconds.

I store start times in startTimeList and end times in endTimeList and iterate through the list to compare the times.

For the purpose of this I wrote this method which compares if the time is within the range of another event's start or end

public boolean isInRange(long timeToBeCompared,long startTime,long endTime){
  if(timeToBeCompared>=startTime&&timeToBeCompared<=endTime){
    return true;
    }
    return false;
  }

I put in first event's starttime or endtime through the above method and pass in second event's start and end time as second and third parameters and I check if its true and use a variable count to hold the count of similar start and end times. However my count value is wrong.

Is there any other good approach?

Это было полезно?

Решение

Try out this code:

/** Checks whether two intervals overlap. */
boolean overlap(long startTime1, long endTime1, long startTime2, long endTime2) {
    if (endTime1 < startTime2 || startTime1 > endTime2) {
        return false;
    return true;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top