我创建了一个活动。现在我想检查它们是否相互重叠。我在数据库中以日期格式存储了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 :(得分:1)
我会使用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);
}
答案 1 :(得分:0)
我建议您将字符串startTime
和endTime
转换为Date
个对象,并使用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
注意,这会计算范围在另一个停止为重叠时正好开始的情况。如果您不这样做,您应该处理开始与另一端相同的情况