日期比較,mysql,代碼如下:
- select * from tb where c> date_format('2007-07-06','%Y%m%d') and c <= date_format('2007-07-09','%Y%m%d');
- select * from tb where c> date('2007-07-07') and c< date('2007-07-09')
STATDAY是形如2006031001的日期數(shù)據(jù)字段,代碼如下:
- select * from LOGINSTAT where STATDAY> date_format(curdate()-1,'%Y%m%d') and STATDAY >= date_format(curdate(),'%Y%m%d');或者:select * from LOGINSTAT where STATDAY> date_format(curdate()-1,'%Y%m%d%H') and STATDAY >= date_format(curdate(),'%Y%m%d%H');
其他用法,代碼如下:
- select * from LOGINSTAT where STATDAY >= date_format('$date1','%Y%m%d%H') and STATDAY < date_format('$date','%Y%m%d%H')
mysql數(shù)據(jù)庫(kù)中存的時(shí)間格式為2008-12-28 18:08:08,現(xiàn)在先要從一個(gè)一個(gè)結(jié)果集rs中獲得一個(gè)日期時(shí)間,我先用rs.getDate()方法試著獲取時(shí)間,結(jié)果只有年月日,無(wú)法獲取小時(shí)、分和秒,最后解決的方法是如下代碼:
- Date time1=new Date(rs.getTimestamp("pub_time").getTime());
- SimpleDateFormat formattime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); --Vevb.com
- String pubtime=formatime.format(time1);
獲得的pubtime為String型,在sql語(yǔ)句中用mysql的時(shí)間函數(shù)date_format('time','format')轉(zhuǎn)換,代碼如下:
- String sqlstr="select * from do_document where pub_time<date_format('"+pubtime+"','%Y-%m-%d %H:%i:%s') order by pub_time desc limit 0,1";
然后執(zhí)行該sql語(yǔ)句就查到了滿足條件的記錄.
新聞熱點(diǎn)
疑難解答
圖片精選