如何检查某个日期时间是否在相邻的行日期之间?

时间:2013-07-19 07:24:44

标签: java android sqlite datetime

我有以下表格列值:

2013-06-28T13:00:00.000Z    Motion
2013-06-28T18:15:00.000Z    Static  (row2)
2013-07-03T13:57:22.000Z    Moving  (row3)
2013-07-03T13:59:00.000Z    Motion

我想检查日期29-06-2013T01:00:00Z29-06-2013T05:00:00Z是否都是静态。同样,我想查看日期30-06-2013T01:00:00Z30-06-2013T05:00:00Z。 显然,如果我们考虑上面的表值,则这是正确的,因为两个日期都在第2行和第3行之间。

如何使用sqlite做到这一点?

我收到的日期为29-06-201330-06-2013。首先,我需要将这些日期的凌晨1点和5点设置为两个日期,然后检查它们是否都是静态

我尝试了以下内容:

Calendar cal1 = Calendar.getInstance();
cal1.setTime(input1.parse(startDate.get(0))); //29-06-2013
cal1.add(Calendar.AM, 1); //Added 1am to date 29-06-2013
weekHours = cal1.getTime(); 

Cursor chkCur1 = dbAdapter.rawQuery("select movement from user where Date <= '"
    + input1.format(weekHours) + "' order by Date asc LIMIT 1", null);

chkCur1.moveToFirst();

if(!chkCur1.isAfterLast() && chkCur1.getString(0).equals("Static"))
{
cal1.add(Calendar.AM, 5); //Added 5am to date 29-06-2013
weekHours = cal1.getTime();
}

chkCur1 = dbAdapter.rawQuery("select movement from user where Date <= '"
    + input1.format(weekHours) + "' order by Date asc LIMIT 1", null);

chkCur1.moveToFirst();

if(!chkCur1.isAfterLast() && chkCur1.getString(0).equals("Static")){
cal1.add(Calendar.DATE, 1);
cal1.add(Calendar.AM, 1); //Added 1am to date 30-06-2013
weekHours = cal1.getTime();


}

chkCur1 = dbAdapter.rawQuery("select movement from user where Date <= '"
    + input1.format(weekHours) + "' order by Date asc LIMIT 1", null);

chkCur1.moveToFirst();

if(!chkCur1.isAfterLast() && chkCur1.getString(0).equals("Static")){
cal1.add(Calendar.AM, 5); //Added 5am to date 30-06-2013
weekHours = cal1.getTime();

}

chkCur1 = dbAdapter.rawQuery("select movement from user where Date <= '"
    + input1.format(weekHours) + "' order by Date asc LIMIT 1", null);

chkCur1.moveToFirst();

if(!chkCur1.isAfterLast() && chkCur1.getString(0).equals("Static")){
System.out.println("Both dates are in between")
}

我使用了4个SQL查询:

1)将时间1am设置为date1并检查其上一个是否为静态。

2)将时间5am设置为date1并检查其上一个是否为静态。

3)将时间1am设置为date2并检查其上一个是否为静态。

4)将时间5am设置为date2并检查其上一个是否为静态。

如果所有条件都满足,则两个日期(date1和date2)介于两者之间,即静态。

但这是sql查询的重复,从而减慢了进程。

1 个答案:

答案 0 :(得分:1)

您可以将所有四项检查放入子查询中:

SELECT (SELECT movement FROM user WHERE Date <= ? ORDER BY Date) = 'Static'
   AND (SELECT movement FROM user WHERE Date <= ? ORDER BY Date) = 'Static'
   AND (SELECT movement FROM user WHERE Date <= ? ORDER BY Date) = 'Static'
   AND (SELECT movement FROM user WHERE Date <= ? ORDER BY Date) = 'Static';

这将为您提供一个带有单个布尔列的结果记录。