我用的是hibernate没有用其他的框架!还有to_Date我试了在mysql里不行,直接报错的

public List getTime(DeviceHistory beTime,DeviceHistory endTime)throws Exception {
List l=new ArrayList();
Session s = this.getSession();
Query q=s.createQuery("from DeviceHistory d where d.DId=:dd and d.deviceTime between :aa and :bb");
q.setDate("aa", beTime.getDeviceTime());
q.setDate("bb", endTime.getDeviceTime());
l=q.list();
s.close();
return l;
}
public static void main(String[] args) throws Exception{
List l= new ArrayList();
DeviceHistoryDao dgd=new DeviceHistoryDao();
ToChange tc=new ToChange();
DeviceHistory dg=new DeviceHistory();
dg.setDId(1);
// dg.setId(6);
dg.setDeviceTime(tc.charToDate("2008-10-06 13:00"));
DeviceHistory dg1=new DeviceHistory();
dg1.setDId(1);
// dg1.setId(8);
dg1.setDeviceTime(tc.charToDate("2008-10-06 16:00"));
l=dgd.getTime(dg,dg1);

System.out.println("list=="+l.size());
}

public Date charToDate(String s){
DateFormat df=new SimpleDateFormat("yyyy-mm-dd hh:mm");
Date dd=new Date(0);
try {
if(s!=null&&s.trim().length()>0){
dd = (Date) df.parse(s);
}
} catch (Exception e) {
e.printStackTrace();
}
return dd;
}
public class DeviceHistory {

// Fields  

private Integer id;
private Integer DId;
private String lng;
private String lat;
private Float speed;
private String info;
private Date deviceTime;
}


beTime.getDeviceTime()获取的是java.util.Date;类型,在public List getTime(DeviceHistory beTime,DeviceHistory endTime)方法里我少了 q.setInteger("dd", beTime.getDId());这里是我粘贴的时候没有粘上去。

慕后森
浏览 77回答 2
2回答

Helenr

hibernatemysql查询日期范围:呵呵,有必要你在这里用SimpleDateFormat转化日期格式吗?用mysql的TO_DAYS函数就可以了,如下:fromDeviceHistorydwhered.DId=:ddandwhereTO_DAYS(d.deviceTime)>=TO_DAYS("+beginTime+")andTO_DAYS(d.deviceTime)<=TO_DAYS("+endTime+")试试吧,beginTime和endTime就是日期范围mysql的TO_DAYS函数返回的就是天数,你就不要转化日期格式了&nbsp;

温温酱

hibernate mysql 查询日期范围:呵呵,有必要你在这里用SimpleDateFormat转化日期格式吗?用mysql的TO_DAYS函数就可以了,如下:from DeviceHistory d where d.DId=:dd and where TO_DAYS(d.deviceTime) >= TO_DAYS("+beginTime+") and TO_DAYS(d.deviceTime) <= TO_DAYS("+endTime+")&nbsp;试试吧,beginTime和endTime就是日期范围mysql的TO_DAYS函数返回的就是天数,你就不要转化日期格式了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

MySQL