oracle 按時間查詢可以有2種形式
to_date and to_char
for example:
// 將數據庫字段轉成char 進行查詢
select * from tableName where to_char(字段名,'yyyy-mm-dd')<='2015-11-11'
//將條件轉成date進行查詢
select * from tableNmae where 字段名<=to_date('2015-11-11','yyyy-mm-dd')
兩者唯一的區別是 前者能查到11-11的數據。
與java結合
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
String time=df.format(new Date());//獲取當前時間
String sql= "select * from tbl_holly_tdlist where to_char( time,'yyyy-mm-dd')<='"+time+"'";
業務需要獲取當前一年的時間的數據
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式
Calendar calendar =df.getCalendar();
Date date = new Date();
calendar.setTime(date);
calendar.add(Calendar.YEAR, -1);// 將時間推前一年。
date = calendar.getTime();
String sql = "";
sql+="select * from tbl_holly_tdlist where to_char( time,'yyyy-mm-dd')>='"+df.format(date)+"' order by time desc";
第一次寫,主要是爲了以後自己查閱方面,如能幫到別人,那就更好了。