要獲取Android系統(tǒng)時(shí)間,可以使用以下方法之一:
System.currentTimeMillis()
方法獲取當(dāng)前時(shí)間的毫秒值。long currentTimeMillis = System.currentTimeMillis();
Calendar.getInstance()
獲取Calendar對(duì)象,并從中獲取系統(tǒng)時(shí)間的各個(gè)字段。Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
SimpleDateFormat
類將毫秒值轉(zhuǎn)換為特定格式的時(shí)間字符串。SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date(System.currentTimeMillis()));
這些方法可以幫助你在Android應(yīng)用中獲取系統(tǒng)時(shí)間。