天天看點

Android 擷取系統時間的三種方式

Android擷取目前時間

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22

取得系統時間

1.1

SimpleDateFormat formatter= new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss ");       

Date curDate = new Date(System.currentTimeMillis());//擷取目前時間       

String str = formatter.format(curDate);    

1.2

SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");       

String date= sDateFormat.format(new Date());  

1.3 單獨擷取年月或者時分

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM");    

String date=sdf.format(new Date());  

SimpleDateFormat sdf=new SimpleDateFormat("hh:mm:ss");    

String date=sdf.format(new Date());

2

final

Calendar mCalendar=Calendar.getInstance();

mCalendar.setTimeInMillis(time);

取得小時:mHour=mCalendar.get(Calendar.HOUR);

取得分鐘:mMinuts=mCalendar.get(Calendar.MINUTE);

取得系統日期:

year = c.get(Calendar.YEAR)                  month = c.grt(Calendar.MONTH)                  day = c.get(Calendar.DAY_OF_MONTH)    

3

Time t=

new

Time();

t.setToNow();// 取得系統時間。 int year = t.year; int month = t.month; int date = t.monthDay; int hour = t.hour;  

繼續閱讀