我面临一些与日期相关的小问题。实际上我正在使用 grails 2.3.7 并尝试根据日期范围查找记录。假设我想获取日期 26-11-2014 的所有记录。
所以我最终使用了这种方法。
String fromDate="2014-12-03";
String toDate="2014-12-04";
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd")
Date d1 = df1.parse(fromDate);
Date d2 = df1.parse(toDate);
Order?.executeQuery("select orderDate from Order where orderDate between ? and ? ",[d1,d2]);
结果是
[2014-12-03 10:27:23.0, 2014-12-03 10:30:53.0, 2014-12-03 10:32:30.0]
实际上 2014 年 3 月 12 日有 4 条记录,但我们只得到了 3 条。
同样的情况适用于2014年11月27日至2014年11月28日
结果是
[2014-11-27 10:52:56.0, 2014-11-27 13:42:16.0, 2014-11-27 14:44:05.0, 2014-11-27 15:47:36.0,
2014-11-27 15:51:36.0, 2014-11-27 16:10:17.0, 2014-11-27 18:01:08.0, 2014-11-27 18:07:47.0,
2014-11-27 18:30:54.0, 2014-11-27 18:34:06.0, 2014-11-27 18:40:35.0]
实际上 2014 年 11 月 27 日有 13 条记录,但我们只得到了 11 条。
请帮助我。
我被困在这里了。
最佳答案
问题出在日期的时间部分。
您可以执行以下操作来检索 orderDate = 2014-12-03 的所有行:
String fromDate="2014-12-03 00:00:00"
String toDate="2014-12-03 23:59:59"
SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
Date d1 = df1.parse(fromDate)
Date d2 = df1.parse(toDate)
Order?.executeQuery("select orderDate from Order where orderDate between ? and ? ",[d1,d2]);
您还可以使用 Date.clearTime() 删除日期的时间。
关于java - 如何在grails中根据日期获取记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27330558/