溫馨提示×

android怎么獲取系統時間

小億
134
2024-07-27 02:07:08
欄目: 編程語言

要獲取Android系統時間,可以使用以下方法之一:

  1. 使用System.currentTimeMillis()方法獲取當前時間的毫秒值。
long currentTimeMillis = System.currentTimeMillis();
  1. 使用Calendar.getInstance()獲取Calendar對象,并從中獲取系統時間的各個字段。
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);
  1. 使用SimpleDateFormat類將毫秒值轉換為特定格式的時間字符串。
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(new Date(System.currentTimeMillis()));

這些方法可以幫助你在Android應用中獲取系統時間。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女