读书人

组合查询及时间有关问题

发布时间: 2012-11-06 14:07:00 作者: rapoo

组合查询及时间问题

经常会遇到一些组合查询:常用格式如下;StringBuffer buff = new StringBuffer();buff.append("from YlrkData ");if(ckName!=null && !ckName.equals("")){   buff.append("where biaozhi='"+ckName+"'");}if(yclName!=null && !yclName.equals("")){   buff.append("and name='"+yclName+"'");}//把结束时间加1,不然的话查不出当天的数据。因为查询条件是<=结束日期,比如3月4号,而有条记录是3月4号12点录入的,就会查不出来!所以要加1if(endTime!=null&&!endTime.equals("")){  DateFormat format = new SimpleDateFormat("yyyy-MM-dd");  Date endT = format.parse(endTime);  Calendar calendar=Calendar.getInstance();  calendar.setTime(endT);  calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH)+1);  endTime=format.format(calendar.getTime());}if(endTime!=null&&!endTime.equals("")){   buff.append("and rkrq<='"+endTime+"'");}

有时数据库中的时间存入了时分秒,但业务逻辑上不需要,可以用string.substring(0,10)来得到年月日。

//得到两个日期之间的天数public long getDay(Date start,Date end){long day = 0;day=(end.getTime()-start.getTime())/(24*60*60*1000);return day;}

读书人网 >软件架构设计

热点推荐