2008-03-19
hibernate日期查询引发的问题
下面的代码是查询某两个日期间的记录
不知道为什么 第一种方法就是查不出结果来 (注掉的代码)
另外下面两个方法是获取一天的开始和结束时间
感觉写的挺麻烦的 不知道有没有更简单的方法
还有一点就是SimpleDateFormat格式化时要注意的问题: HH大些输出24小时格式 hh小写是12小时
下班了 撤退!
在使用SimpleDateFormat时格式化时间的 yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的,则使用hh:mm:ss 如果希望格式化时间为24小时制的,则使用HH:mm:ss
public int countTodayLogin(){
// return (Integer)createQuery("select count(*) from User where lastLoginTime between ? and ?")
// .setDate(0, this.getStartOfToday())
// .setDate(1, this.getEndOfToday())
// .setCacheable(true)
// .uniqueResult();
return (Integer)createCriteria()
.add(Restrictions.between("lastLoginTime",getStartOfToday(),getEndOfToday()))
.setProjection(Projections.count("id"))
.uniqueResult();
}
不知道为什么 第一种方法就是查不出结果来 (注掉的代码)
另外下面两个方法是获取一天的开始和结束时间
感觉写的挺麻烦的 不知道有没有更简单的方法
private Date getStartOfToday(){
Calendar today = Calendar.getInstance();
int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
int day = today.get(Calendar.DAY_OF_MONTH);
return new GregorianCalendar(year, month, day, 0, 0, 0).getTime();
}
private Date getEndOfToday(){
Calendar today = Calendar.getInstance();
int year = today.get(Calendar.YEAR);
int month = today.get(Calendar.MONTH);
int day = today.get(Calendar.DAY_OF_MONTH);
return new GregorianCalendar(year, month, day, 23, 59, 59).getTime();
}
还有一点就是SimpleDateFormat格式化时要注意的问题: HH大些输出24小时格式 hh小写是12小时
下班了 撤退!
在使用SimpleDateFormat时格式化时间的 yyyy.MM.dd 为年月日而如果希望格式化时间为12小时制的,则使用hh:mm:ss 如果希望格式化时间为24小时制的,则使用HH:mm:ss
发表评论
- 浏览: 81261 次
- 性别:

- 来自: 北京

- 详细资料
搜索本博客
我的相册
u=3528569133,1587051000&gp=38.jpg
共 2 张
共 2 张
最新评论
-
struts2 配置文件 struts. ...
gdsagdsa
-- by biti910 -
struts2 配置文件 struts. ...
<h1>嘿嘿</h1>
-- by biti910 -
spring中OpenSessionInVie ...
<bean name="openSessionInViewInte ...
-- by davidcen -
关于文章点击量的缓存
bloodrate 写道那得多大得cache啊?每个帖子对应map里得一条记录, ...
-- by robbin -
关于文章点击量的缓存
那得多大得cache啊?每个帖子对应map里得一条记录,以id为key查,估计有 ...
-- by bloodrate






评论排行榜