可以獲得當前的年月時分秒
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());
利用Calendar獲取
Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); month = c.grt(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE);
利用Time獲取
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone資料。
t.setToNow(); // 取得系統時間。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
int minute = t.minute;
int second = t.second;
沒有留言 :
張貼留言