當前位置:首頁 » 安卓系統 » android取時間

android取時間

發布時間: 2023-03-29 18:53:35

① android怎麼獲得當前時間

取得系統時間:
1。long time=System.currentTimeMillis();

2。final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小時:mHour=mCalendar.get(Calendar.HOUR);
取得分鍾:mMinuts=mCalendar.get(Calendar.MINUTE);

3。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

4。DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());

② Android 怎麼獲取當前的時間戳

Android獲取當前時間代碼

//需要引用的
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

//詳細代碼
java.util.Date currentdate = new java.util.Date();//當前時間
//long i = (currentdate.getTime()/1000-timestamp)/(60);
//System.out.println(currentdate.getTime());
//System.out.println(i);
Timestamp now = new Timestamp(System.currentTimeMillis());//獲取系統當前時間
System.out.println("now-->"+now);//返回結果精確到毫秒。

時間戳轉日期
int timestamp = 1310457552; //將這個時間戳轉為日期

return getTime(timestamp);

定義getTime, getDate, IntToLong

public static String getTime(int timestamp){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time= null;
try {
String str = sdf.format(new Timestamp(IntToLong(timestamp)));
time = str.substring(11, 16);

String month = str.substring(5, 7);
String day = str.substring(8,10 );
time =getDate(month, day)+ time;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return time;
}

public static String getDate(String month,String day){
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//24小時制
java.util.Date d = new java.util.Date(); ;
String str = sdf.format(d);
String nowmonth = str.substring(5, 7);
String nowday = str.substring(8,10 );
String result = null;

int temp = Integer.parseInt(nowday)-Integer.parseInt(day);
switch (temp) {
case 0:
result="今天";
break;
case 1:
result = "昨天";
break;
case 2:
result = "前天";
break;
default:
StringBuilder sb = new StringBuilder();
sb.append(Integer.parseInt(month)+"月");
sb.append(Integer.parseInt(day)+"日");
result = sb.toString();
break;
}
return result;
}

//java Timestamp構造函數需傳入Long型
public static long IntToLong(int i){
long result = (long)i;
result*=1000;
return result;
}

③ android獲取系統時間並顯示

import java.util.Calendar;
import android.app.Activity;import android.os.Bundle;
public class GetSystemTime001Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Calendar c=Calendar.getInstance();
String time=c.get(Calendar.YEAR)+"-"+ //得到年
formatTime(c.get(Calendar.MONTH)+1)+"-"+//month加一 //月
formatTime(c.get(Calendar.DAY_OF_MONTH))+" "+ //日
formatTime(c.get(Calendar.HOUR_OF_DAY))+":"+ //時
formatTime(c.get(Calendar.MINUTE))+":"+ //分
formatTime(c.get(Calendar.SECOND)); //秒
System.out.println(time); //輸出
}
private String formatTime(int t){
return t>=10? ""+t:"0"+t;//三元運算符 t>10時取 ""+t
}
}

④ Android 獲取路由器時間

通過桌面點擊「設置」App,進入「設置」功能列表。

在「設置」功能列表中,點擊「系統和更新」。

在「系統和更新」功能列表中,點擊「日期和時間」。

在「日期和時間」功能窗口,點擊「自動設置日期、時間和時區」標簽右側的功能按鈕,啟用該功能,系統即可通過網路自動校準時間和日期。

⑤ android 獲取時間需要許可權嗎

不需要許可權

取得系統時間
1。
long time=System.currentTimeMillis();

2。
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小時:mHour=mCalendar.get(Calendar.HOUR);
取得分鍾:mMinuts=mCalendar.get(Calendar.MINUTE);

3。
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

4。
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());

⑥ 怎麼在android中獲取當前時間

首先開網,設置 時間 自動獲取網路時間(大至是那個意思的) 你會發現時間設置選項變暗 現在再點擊幾點幾點那個 然後再採納

熱點內容
手機點菜app怎麼連接電腦伺服器 發布:2025-07-05 11:13:05 瀏覽:942
配置控制台干什麼用的 發布:2025-07-05 10:54:51 瀏覽:961
連信從哪裡改登錄密碼 發布:2025-07-05 10:54:12 瀏覽:398
怎麼修改查詢密碼 發布:2025-07-05 10:49:48 瀏覽:967
matlab文件存儲 發布:2025-07-05 10:40:46 瀏覽:85
梅州市用工實名制管理平台雲存儲 發布:2025-07-05 10:28:59 瀏覽:77
安卓origin怎麼設置 發布:2025-07-05 10:20:10 瀏覽:542
安卓為什麼跳水 發布:2025-07-05 09:55:08 瀏覽:88
達內學校php 發布:2025-07-05 09:52:05 瀏覽:399
獲取資料庫所有表 發布:2025-07-05 09:39:12 瀏覽:655