Posted on 2012-04-11 13:10
小白19870626 閱讀(225)
評論(0) 編輯 收藏 所屬分類:
java
import java.text.SimpleDateFormat;
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
Date curDate = new Date(System.currentTimeMillis());//獲取當前時間
String str = formatter.format(curDate);
可以獲取當前的年月時分,也可以分開寫:
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String date = sDateFormat.format(new java.util.Date());
如果想獲取當前的年月,則可以這樣寫(只獲取時間或秒種一樣):
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");
String date=sdf.format(new java.util.Date());
當然還有就是可以指定時區的時間(待):
df=DateFormat.getDateTimeInstance(DateFormat.FULL,DateFormat.FULL,Locale.CHINA);
System.out.println(df.format(new Date()));
小白