這是在一個群裡討論的問題
方法很多如果說數據庫中數據的存儲是用date類型的話改怎麼做如果說做數據遷移的話又該怎麼做?
最終的討論方案在用字符串存儲格式上比較好畢竟各個數據庫對時間的存儲存在差異
一下是我用hibernate中的HQL和QBC兩種檢索方式對一段時間內的數據進行檢索
package comdudulili;
import javautilDate;
import javautilIterator;
import javautilList;
import orghibernateCriteria;
import orghibernateSession;
import orghibernateTransaction;
import orghibernatecriterionCriterion;
import orghibernatecriterionExpression;
public class Select {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Autogenerated method stub
Session session = HibernateSessionFactorygetSession();
Transaction tran = sessionbeginTransaction();
/*
* 采用HQL的方式
Date begin = javasqlDatevalueOf();
Date end = javasqlDatevalueOf();
List<A> result = sessioncreateQuery(from A a where adate > :beginTime and adate <= :endTime)setTimestamp(beginTime begin)setTimestamp(endTime end)list();
for(A a : result){
Systemoutprintln(agetId());
Systemoutprintln(agetTitle());
Systemoutprintln(agetDate());
}
*/
// 采用QBC的方式
Date begin = javasqlDatevalueOf();
Date end = javasqlDatevalueOf();
Criteria criteria = sessioncreateCriteria(AbstractAclass);
Criterion creterion = Expressionbetween(date begin end);
List<A> result = criteriaadd(creterion)list();
for(A a : result){
Systemoutprintln(agetId());
Systemoutprintln(agetTitle());
Systemoutprintln(agetDate());
}
}
}
From:http://tw.wingwit.com/Article/program/Java/ky/201311/27940.html