oracle及Java日期格式

若是想轉化爲2005-07-05 的格式,則須要使用類 SimpleDateFormatsession

  SimpleDateFormat formatt= new SimpleDateFormat("yyyy-MM-dd") ;
 Date date =new Date();
 System.out.println(formatt.format(date));
oracle

 則打印出的格式爲:2005-07-07函數

  SimpleDateFormat formatt= new SimpleDateFormat("yyyy-MMMM-dd") ;
 Date date =new Date();
 System.out.println(formatt.format(date));
this

  則打印出的格式爲:2005-七月-07code

---------------------------orm

Oracle的默認日期格式 ci

SQL> select sysdate from dual ;字符串

SYSDATE
----------
07-7月-05
it

用to_char轉化爲yyyy-mm-dd的格式io

SQL> select to_char(sysdate,'yyyy-mm-dd') Time from dual ;
TIME
----------
2005-07-07

在Oracle裏有兩個與date相關的函數 to_date()和to_char() ;

to_date() 做用將字符類型按必定格式轉化爲日期類型:
具體用法:to_date('2004-11-27','yyyy-mm-dd'),前者爲字符串,後者爲轉換日期格式,注意,先後二者要以一對應。
如;to_date('2004-11-27 13:34:43', 'yyyy-mm-dd hh24:mi:ss') 將獲得具體的時間
to_date() 做用將字符類型按必定格式轉化爲日期類型:
具體用法:to_date('2004-11-27','yyyy-mm-dd'),前者爲字符串,後者爲轉換日期格式,注意,先後二者要以一對應。
如;to_date('2004-11-27 13:34:43', 'yyyy-mm-dd hh24:mi:ss') 將獲得具體的時間.

YYYY:四位表示的年份 
YYY,YY,Y:年份的最後三位、兩位或一位,缺省爲當前世紀 
MM:01~12的月份編號 
MONTH:九個字符表示的月份,右邊用空格填補 
MON:三位字符的月份縮寫 
WW:一年中的星期 
D:星期中的第幾天 
DD:月份中的第幾天 
DDD:年所中的第幾天 
DAY:九個字符表示的天的全稱,右邊用空格補齊 
HH,HH12:一天中的第幾個小時,12進製表示法 
HH24:一天中的第幾個小時,取值爲00~23 
MI:一小時中的分鐘 
SS:一分鐘中的秒 
SSSS:從午夜開始過去的秒數 

 to_char():將日期轉按必定格式換成字符類型

1.  日期和字符轉換函數用法(to_date,to_char)  2.  select to_char( to_date(222,'J'),'Jsp') from dual  顯示Two Hundred Twenty-Two  3.  求某天是星期幾  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day') from dual;  星期一  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;  monday  設置日期語言  ALTER SESSION SET NLS_DATE_LANGUAGE='AMERICAN';  也能夠這樣  TO_DATE ('2002-08-26', 'YYYY-mm-dd', 'NLS_DATE_LANGUAGE = American')  4.  兩個日期間的天數  select floor(sysdate - to_date('20020405','yyyymmdd')) from dual;  5. 時間爲null的用法  select id, active_date from table1  UNION  select 1, TO_DATE(null) from dual;  注意要用TO_DATE(null)  6.  a_date between to_date('20011201','yyyymmdd') and to_date('20011231','yyyymmdd')  那麼12月31號中午12點以後和12月1號的12點以前是不包含在這個範圍以內的。  因此,當時間須要精確的時候,以爲to_char仍是必要的  7. 日期格式衝突問題  輸入的格式要看你安裝的ORACLE字符集的類型, 好比: US7ASCII, date格式的類型就是: '01-Jan-01'  alter system set NLS_DATE_LANGUAGE = American  alter session set NLS_DATE_LANGUAGE = American  或者在to_date中寫  select to_char(to_date('2002-08-26','yyyy-mm-dd'),'day','NLS_DATE_LANGUAGE = American') from dual;  注意我這只是舉了NLS_DATE_LANGUAGE,固然還有不少,  可查看  select * from nls_session_parameters  select * from V$NLS_PARAMETERS  8.  select count(*)  from ( select rownum-1 rnum  from all_objects  where rownum <= to_date('2002-02-28','yyyy-mm-dd') - to_date('2002-  02-01','yyyy-mm-dd')+1  )  where to_char( to_date('2002-02-01','yyyy-mm-dd')+rnum-1, 'D' )  not  in ( '1', '7' )  查找2002-02-28至2002-02-01間除星期一和七的天數  在先後分別調用DBMS_UTILITY.GET_TIME, 讓後將結果相減(獲得的是1/100秒, 而不是毫秒).  9.  select months_between(to_date('01-31-1999','MM-DD-YYYY'),  to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;  1  select months_between(to_date('02-01-1999','MM-DD-YYYY'),  to_date('12-31-1998','MM-DD-YYYY')) "MONTHS" FROM DUAL;  1.03225806451613  10. Next_day的用法  Next_day(date, day)  Monday-Sunday, for format code DAY  Mon-Sun, for format code DY  1-7, for format code D  11  select to_char(sysdate,'hh:mi:ss') TIME from all_objects  注意:第一條記錄的TIME 與最後一行是同樣的  能夠創建一個函數來處理這個問題  create or replace function sys_date return date is  begin  return sysdate;  end;  select to_char(sys_date,'hh:mi:ss') from all_objects;  12.  得到小時數  SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 2:38:40') from offer  SQL> select sysdate ,to_char(sysdate,'hh') from dual;  SYSDATE TO_CHAR(SYSDATE,'HH')  -------------------- ---------------------  2003-10-13 19:35:21 07  SQL> select sysdate ,to_char(sysdate,'hh24') from dual;  SYSDATE TO_CHAR(SYSDATE,'HH24')  -------------------- -----------------------  2003-10-13 19:35:21 19  獲取年月日與此相似  13.  年月日的處理  select older_date,  newer_date,  years,  months,  abs(  trunc(  newer_date-  add_months( older_date,years*12+months )  )  ) days  from ( select  trunc(months_between( newer_date, older_date )/12) YEARS,  mod(trunc(months_between( newer_date, older_date )),  12 ) MONTHS,  newer_date,  older_date  from ( select hiredate older_date,  add_months(hiredate,rownum)+rownum newer_date  from emp )  )  14.  處理月份天數不定的辦法  select to_char(add_months(last_day(sysdate) +1, -2), 'yyyymmdd'),last_day(sysdate) from dual  16.  找出今年的天數  select add_months(trunc(sysdate,'year'), 12) - trunc(sysdate,'year') from dual  閏年的處理方法  to_char( last_day( to_date('02' || :year,'mmyyyy') ), 'dd' )  若是是28就不是閏年  17.  yyyy與rrrr的區別  'YYYY99 TO_C  ------- ----  yyyy 99 0099  rrrr 99 1999  yyyy 01 0001  rrrr 01 2001  18.不一樣時區的處理  select to_char( NEW_TIME( sysdate, 'GMT','EST'), 'dd/mm/yyyy hh:mi:ss') ,sysdate  from dual;  19.  5秒鐘一個間隔  Select TO_DATE(FLOOR(TO_CHAR(sysdate,'SSSSS')/300) * 300,'SSSSS') ,TO_CHAR(sysdate,'SSSSS')  from dual  2002-11-1 9:55:00 35786  SSSSS表示5位秒數  20.  一年的第幾天  select TO_CHAR(SYSDATE,'DDD'),sysdate from dual  310 2002-11-6 10:03:51  21.計算小時,分,秒,毫秒  select  Days,  A,  TRUNC(A*24) Hours,  TRUNC(A*24*60 - 60*TRUNC(A*24)) Minutes,  TRUNC(A*24*60*60 - 60*TRUNC(A*24*60)) Seconds,  TRUNC(A*24*60*60*100 - 100*TRUNC(A*24*60*60)) mSeconds  from  (  select  trunc(sysdate) Days,  sysdate - trunc(sysdate) A  from dual  )  select * from tabname  order by decode(mode,'FIFO',1,-1)*to_char(rq,'yyyymmddhh24miss');  //  floor((date2-date1) /365) 做爲年  floor((date2-date1, 365) /30) 做爲月  mod(mod(date2-date1, 365), 30)做爲日.  23.next_day函數  next_day(sysdate,6)是從當前開始下一個星期五。後面的數字是從星期日開始算起。  1 2 3 4 5 6 7  日 一 二 三 四 五 六oracle中有不少關於日期的函數在oracle中有不少關於日期的函數,如: 一、add_months()用於從一個日期值增長或減小一些月份 date_value:=add_months(date_value,number_of_months) 例: SQL> select add_months(sysdate,12) "Next Year" from dual; Next Year ---------- 13-11月-04 SQL> select add_months(sysdate,112) "Last Year" from dual; Last Year ---------- 13-3月 -13 SQL> 二、current_date()返回當前會放時區中的當前日期 date_value:=current_date SQL> column sessiontimezone for a15 SQL> select sessiontimezone,current_date from dual; SESSIONTIMEZONE CURRENT_DA --------------- ---------- +08:00 13-11月-03 SQL> alter session set time_zone='-11:00'   2 / 會話已更改。 SQL> select sessiontimezone,current_timestamp from dual; SESSIONTIMEZONE CURRENT_TIMESTAMP --------------- ------------------------------------ -11:00 12-11月-03 04.59.13.668000 下午 -11:                 00 SQL> 三、current_timestamp()以timestamp with time zone數據類型返回當前會放時區中的當前日期 timestamp_with_time_zone_value:=current_timestamp([timestamp_precision]) SQL> column sessiontimezone for a15 SQL> column current_timestamp format a36 SQL> select sessiontimezone,current_timestamp from dual; SESSIONTIMEZONE CURRENT_TIMESTAMP --------------- ------------------------------------ +08:00 13-11月-03 11.56.28.160000 上午 +08:                 00 SQL> alter session set time_zone='-11:00'   2 / 會話已更改。 SQL> select sessiontimezone,current_timestamp from dual; SESSIONTIMEZONE CURRENT_TIMESTAMP --------------- ------------------------------------ -11:00 12-11月-03 04.58.00.243000 下午 -11:                 00 SQL> 四、dbtimezone()返回時區 varchar_value:=dbtimezone SQL> select dbtimezone from dual; DBTIME ------ -07:00 SQL> 五、extract()找出日期或間隔值的字段值 date_value:=extract(date_field from [datetime_value|interval_value]) SQL> select extract(month from sysdate) "This Month" from dual; This Month ----------         11 SQL> select extract(year from add_months(sysdate,36)) "3 Years Out" from dual; 3 Years Out -----------        2006 SQL> 六、last_day()返回包含了日期參數的月份的最後一天的日期 date_value:=last_day(date_value) SQL> select last_day(date'2000-02-01') "Leap Yr?" from dual; Leap Yr? ---------- 29-2月 -00 SQL> select last_day(sysdate) "Last day of this month" from dual; Last day o ---------- 30-11月-03 SQL> 七、localtimestamp()返回會話中的日期和時間 timestamp_value:=localtimestamp SQL> column localtimestamp format a28 SQL> select localtimestamp from dual; LOCALTIMESTAMP ---------------------------- 13-11月-03 12.09.15.433000 下午 SQL> select localtimestamp,current_timestamp from dual; LOCALTIMESTAMP CURRENT_TIMESTAMP ---------------------------- ------------------------------------ 13-11月-03 12.09.31.006000 13-11月-03 12.09.31.006000 下午 +08: 下午 00 SQL> alter session set time_zone='-11:00'; 會話已更改。 SQL> select localtimestamp,to_char(sysdate,'DD-MM-YYYY HH:MI:SS AM') "SYSDATE" from dual; LOCALTIMESTAMP SYSDATE ---------------------------- ------------------------ 12-11月-03 05.11.31.259000 13-11-2003 12:11:31 下午 下午 SQL> 八、months_between()判斷兩個日期之間的月份數量 number_value:=months_between(date_value,date_value) SQL> select months_between(sysdate,date'1971-05-18') from dual; MONTHS_BETWEEN(SYSDATE,DATE'1971-05-18') ----------------------------------------                               389.855143 SQL> select months_between(sysdate,date'2001-01-01') from dual; MONTHS_BETWEEN(SYSDATE,DATE'2001-01-01') ----------------------------------------                               34.4035409 SQL> 九、next_day()給定一個日期值,返回由第二個參數指出的日子第一次出如今的日期值(應返回相應日子的名稱字符串)?周相?日期函?1.查詢某周的第一天select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') last_dayfrom (select substr('2004-32', 1, 4) yy, to_number(substr('2004-32', 6)) ww         from dual)select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d')-6 first_day from dualselect min(v_date) from  (select (to_date('200201','yyyymm') + rownum) v_date  from all_tables  where rownum < 370)where to_char(v_date,'yyyy-iw') = '2002-49'2.查詢某周的最後一天select trunc(decode(ww, 53, to_date(yy || '3112', 'yyyyddmm'), to_date(yy || '-' || to_char(ww * 7), 'yyyy-ddd')), 'd') - 6 first_day  from (select substr('2004-33', 1, 4) yy, to_number(substr('2004-33', 6)) ww          from dual)          select trunc(to_date(substr('2003-01',1,5)||to_char((to_number(substr('2003-01',6)))*7),'yyyy-ddd'),'d') last_day from dualselect max(v_date) from  (select (to_date('200408','yyyymm') + rownum) v_date  from all_tables  where rownum < 370)where to_char(v_date,'yyyy-iw') = '2004-33'3.查詢某周的日期select min_date, to_char(min_date,'day') day from(select to_date(substr('2004-33',1,4)||'001'+rownum-1,'yyyyddd') min_date         from all_tables  where rownum <= decode(mod(to_number(substr('2004-33',1,4)),4),0,366,365)    union  select to_date(substr('2004-33',1,4)-1||         decode(mod(to_number(substr('2004-33',1,4))-1,4),0,359,358)+rownum,'yyyyddd') min_date         from all_tables                    where rownum <= 7  union  select to_date(substr('2004-33',1,4)+1||'001'+rownum-1,'yyyyddd') min_date         from all_tables                    where rownum <= 7                       )where to_char(min_date,'yyyy-iw') ='2004-33'

相關文章
相關標籤/搜索