把日期格式化
import java.text.DateFormat;
import java.text.SimpleDateFormat;
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
String dateString = df.format(new Date());
DateFormat、SimpleDateFormat是非線程安全的,不要放在類屬性上,在方法裡new
數字格式化,這個也是非線程安全的,
DecimalFormat dft = new DecimalFormat("#0.00");
特殊的一種
String strDate = "Wed Jan 16 09:54:44 +0800 2013";
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.ENGLISH);
Date dt = sdf .parse(strDate);
把字元串轉成日期
Date dt= df.parse(dateString);
oracle日期格式化
select to_char(sysdate,'YYYY-MM-DD HH24:MI:SS') from dual;
select to_date('2014-05-01 21:56:52','YYYY-MM-DD HH24:MI:SS') from dual;
毫秒
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ssxff ') from dual;
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss.ff3 ') from dual; --如果不加3會取6位
select to_char(systimestamp,'yyyy-mm-dd hh24:mi:ss.ff ') from dual;