2016-03-13 120 views
0

我創建了一個活動。現在我想檢查它們是否相互重疊。我在數據庫中以日期格式存儲了startTime和endTime。如何檢查時間是否重疊?

現在我想檢查事件劑量的時間是否重疊。爲此,我曾想過要獲取所有事件startTime和endTime,從日期中檢索小時和分鐘,然後將小時和分鐘與當前小時和分鐘進行比較。

日期格式是這樣的:df = new SimpleDateFormat(「E MMM dd HH:mm:ss zzzz yyyy」);

我第一次試圖通過查詢來比較兩個日期,但它並沒有幫上忙。如果事件在當時不存在,那麼有時它也會返回true。

我第一次嘗試是這樣的:

public int returnCount(String startTime, String endTime, String day) 
{ 
    String selectQuery = "SELECT COUNT(*) FROM " + TABLE + " WHERE " + KEY_DAY_OF_WEEK + " = '" + day + "'" + " AND " 

      + "(" + KEY_FROM_DATE + " <= '" + startTime 
      + "' AND '" + startTime + "' <= " + KEY_TO_DATE + ") OR " 

      + " (" + KEY_FROM_DATE + " <= '" + endTime 
      + "' AND '" + endTime + "' <= " + KEY_TO_DATE + ") OR " 

      + " (" + KEY_FROM_DATE + " <= '" + startTime 
      + "' AND '" + endTime + "' <= " + KEY_TO_DATE + ") OR " 

      + " (" + KEY_FROM_DATE + " <= '" + endTime 
      + "' AND '" + startTime + "' <= " + KEY_FROM_DATE + ") OR " 

      + " (" + KEY_TO_DATE + " <= '" + startTime 
      + "' AND '" + endTime + "' <= " + KEY_TO_DATE + ") OR " 

      + " ('" + startTime + "' < " + KEY_FROM_DATE 
      + " AND " + KEY_TO_DATE + " < '" + endTime + "')"; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor mCount= db.rawQuery(selectQuery,null); 

    if (mCount.moveToFirst()) { 
     do { 
      EventData event = new EventData(); 
      count = mCount.getInt(0); 
     } while (mCount.moveToNext()); 
    } 

    Log.d("query",selectQuery); 

    Log.d("count",String.valueOf(count)); 
    mCount.close(); 

    return count; 

} 

所以現在我想做出改變,並比較小時和分鐘。爲此,我的嘗試是將小時和分鐘轉換爲字符串,然後進行比較,但是效果不佳。

public void checkOverlappingEvents() 
{ 

    List<EventData> checkOverlappingEvents = new ArrayList<>(); 

    EventTableHelper eventTableHelper = new EventTableHelper(AddEventActivity.this); 


    checkOverlappingEvents = eventTableHelper.getAllEvents("Mon"); 


    for (EventData e : checkOverlappingEvents) 
    { 

     df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy"); 

     Date checkFromDate = new Date(); 
     Date checkToDate = new Date(); 


     try { 

      checkFromDate = df.parse(e.getFromDate()); 
      checkToDate = df.parse(e.getToDate()); 

     } catch (ParseException ex) { 

     } 


     int startHours = datefrom.getHours(); 
     int endHours = dateto.getMinutes(); 

     String startTime = checkFromDate.getHours() + ":" + checkFromDate.getMinutes(); 
     String endTime = checkToDate.getHours() + ":" + checkToDate.getMinutes(); 

     String startDate = datefrom.getHours() + ":" + datefrom.getMinutes(); 
     String endDate = dateto.getHours() + ":" + dateto.getMinutes(); 

     if(startTime.equals(startDate) && endTime.equals(endDate)) 
     { 
      overlapEvents = true; 
     } 

    } 

} 

getAllEvents查詢:

public List<EventData> getAllEvents(String day) { 
    List<EventData> conList = new ArrayList<EventData>(); 

    String selectQuery = "SELECT * FROM " + TABLE + " WHERE " + KEY_DAY_OF_WEEK + " = '" + day + "'"; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    if (cursor.moveToFirst()) { 
     do { 

      EventData event = new EventData(); 

      event.setId(Integer.parseInt(cursor.getString(0))); 
      event.setTitle(cursor.getString(1)); 
      event.setFromDate(cursor.getString(2)); 
      event.setToDate(cursor.getString(3)); 
      event.setDayOfWeek(cursor.getString(4)); 
      event.setLocation(cursor.getString(5)); 
      event.setNotificationTime(cursor.getString(6)); 
      event.setTableId(Integer.parseInt(cursor.getString(7))); 
      event.setNotificationId(Integer.parseInt(cursor.getString(8))); 
      event.setNotification(cursor.getString(9)); 



      conList.add(event); 
     } while (cursor.moveToNext()); 
    } 

    return conList; 
} 

編輯:

public void checkOverlappingEvents() 
{ 

    List<EventData> checkOverlappingEvents = new ArrayList<>(); 

    EventTableHelper eventTableHelper = new EventTableHelper(AddEventActivity.this); 


    checkOverlappingEvents = eventTableHelper.getAllEvents("Mon"); 


    for (EventData e : checkOverlappingEvents) 
    { 

     df = new SimpleDateFormat("E MMM dd HH:mm:ss zzzz yyyy"); 

     Date checkFromDate = new Date(); 
     Date checkToDate = new Date(); 


     try { 

      checkFromDate = df.parse(e.getFromDate()); 
      checkToDate = df.parse(e.getToDate()); 

     } catch (ParseException ex) { 

     } 

checkToDate.getMinutes();

 if(datefrom.compareTo(checkToDate) == checkFromDate.compareTo(dateto)) 
     { 
      overlapEvents = true; 
     } 

    } 

} 

我這樣做,所以它適用於添加一個新的事件。檢查其他日期是否重疊,但我想更新事件。

如果我使用了這個相同的函數,只改變現有事件的結束時間,那麼因爲開始時間相同,它總是顯示事件存在。如何在進行事件更新時進行比較?

任何人都可以幫助我如何比較小時和分鐘與當前小時和分鐘?

回答

0

我建議你轉換您的字符串startTimeendTimeDate對象,並使用date1.compareTo(date2),它返回1如果date2發生後,-1如果說之前,0同一時間。所以,你將有類似的東西:

if(start1.compareTo(start2) != end1.compareTo(start2)) /*overlaps*/; 

你可以看到它是如何工作的時候,你想象它:

start1--------start2------end1--------end2 → will return true 
start1--------end1------start2--------end2 → will return false 

注意,這個計算的情況下一個範圍開始正是當對方停下,重疊。如果你想,否則,你應該處理情況下,開始是一樣的另一端

+0

請問這種檢查所有重疊的時間? – user5881997

+0

現在我在下午7點到晚上8點有一個活動,現在我試着在下午6點到6點30分再添加一個活動。在這個時候沒有事件仍然是真的。如果我嘗試這種方式 – user5881997

+0

該嘗試的來源?因爲使用Date對象要比自己計算好得多 –

0

我會用Date對象

Date startDate1 = ... 
Date startDate2 = ... 
Date endDate1 = ... 
Date endDate2 = ... 

boolean overlapping = datesOverlap(startDate1, endDate1, startDate2, endDate2); 

private boolean datesOverlap(Date startDate1, endDate1, startDate2, endDate2){ 
    return startDate1.before(endDate2) && startDate2.before(endDate1); 
} 
+0

與之前和之前有什麼區別?@Joe Maher – user5881997

+0

@ user5881997比較它們,返回-1(之前),0(同一時間)或1(之後),並明確之前檢查是否是之前和之前我們需要在這種情況下 –

+0

好吧,這是很好的信息。你能檢查我編輯的問題,並幫助我一樣嗎? @Joe Maher – user5881997