gpt4 book ai didi

java - 如何检查时间是否重叠?

转载 作者:行者123 更新时间:2023-11-30 10:48:04 29 4
gpt4 key购买 nike

我创建了一个事件。这些现在我想检查它们是否相互重叠。我在数据库中以日期格式存储了 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;
}

}

}

我这样做是为了添加新事件。检查其他日期是否重叠,但我也想更新事件。

如果我使用相同的功能并且只更改现有事件的结束时间,那么由于开始时间相同,它始终显示事件存在。如何在更新事件时进行比较?

谁能帮助我如何将小时和分钟与当前小时和分钟进行比较?

最佳答案

我会使用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);
}

关于java - 如何检查时间是否重叠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35967487/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com