Timestamp的計算一般是先使用gettime()將其轉化為long再計算。
如下面這個例子:
Timestamp begin = new java.sql.Timestamp((new java.util.Date()).getTime());
java.sql.Timestamp end = new Timestamp(0);
long intime=begin.getTime();
long endtime=intime+((long)RemindDay*1000*24*60*60);//注意將在RemindDay前加上(long)強制類型轉換。
end.setTime(endtime);
是求將當前時間加上RemindDay天后的Timestamp。在沒有加強制類型轉換的情況下,當Timestamp>=25時,計算就會出錯。
因為“RemindDay*1000*24*60*60”都是int類型,其結果也默認是int類型,但是Timestamp>=25時其結果會超過int的最大限制。
所以加個(long)強制類型轉換就可以了。
順便提一下:
使用hibernate查詢在一段時間內的數據的hpl語句是:
Timestamp begin = Timestamp.valueOf("2011-12-05 0:0:0");
Timestamp end = Timestamp.valueOf("2011-12-30 0:0:0");
String hql="from Whstock as wh where wh.endDate between '"+begin+"' and '"+end+"'";