我創建了一個活動。現在我想檢查它們是否相互重疊。我在數據庫中以日期格式存儲了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;
}
}
}
我這樣做,所以它適用於添加一個新的事件。檢查其他日期是否重疊,但我想更新事件。
如果我使用了這個相同的函數,只改變現有事件的結束時間,那麼因爲開始時間相同,它總是顯示事件存在。如何在進行事件更新時進行比較?
任何人都可以幫助我如何比較小時和分鐘與當前小時和分鐘?
請問這種檢查所有重疊的時間? – user5881997
現在我在下午7點到晚上8點有一個活動,現在我試着在下午6點到6點30分再添加一個活動。在這個時候沒有事件仍然是真的。如果我嘗試這種方式 – user5881997
該嘗試的來源?因爲使用Date對象要比自己計算好得多 –