date轉string
A是date格式直接轉換爲字符串格式即varchar2
to_char(A, 'yyyy-mm-dd hh24:mi:ss')
B是字符串varchar2格式先轉換爲date格式再轉換爲字符串格式
改變格式由yyyy-MM-dd改爲yyyyMMdd
to_char(to_date(B, 'yyyy-MM-dd'),'yyyyMMdd')
string轉date
前者爲字符串,後者爲轉換日期格式,兩個參數的格式必須匹配,否則會報錯。
to_date('2018-01-09','yyyy-MM-dd')
to_date('2018-02-23 15:33:21','yyyy-MM-dd HH24:mi:ss')
string格式的to_timestamp數據如 "19-4月 -17 12.00.00.000000000 上午"轉換爲date格式。
to_date(to_char(to_timestamp(T1.CLNDSYRQ, 'dd-mon-rr hh.mi.ss.ff am'),'yyyyMMdd'),'yyyyMMdd')
date和timestamp之間的相互轉換
to_char來轉換timestamp-->date:
select to_date(to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') from dual
date -->timestamp:
select to_timestamp(to_char(sysdate,'yyyy-mm-dd hh24:mi:ss'),'yyyy-mm-dd hh24:mi:ss') from dual
java中轉換
//String—>Date
String time = “2018-01-09”;
SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(time);
//Date—>String
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=new Date();
String str=sdf.format(date);