当前位置:首页 » 安卓系统 » 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中获取当前时间

首先开网,设置 时间 自动获取网络时间(大至是那个意思的) 你会发现时间设置选项变暗 现在再点击几点几点那个 然后再采纳

热点内容
php判断设备 发布:2025-09-17 18:27:53 浏览:399
医院存储方案 发布:2025-09-17 18:26:38 浏览:349
用户注册java 发布:2025-09-17 18:23:40 浏览:395
centosftp不能上传 发布:2025-09-17 18:19:15 浏览:732
我的世界超平坦生存服务器 发布:2025-09-17 18:01:06 浏览:757
手机只上微信要什么配置 发布:2025-09-17 17:56:19 浏览:693
什么叫配置高的电脑 发布:2025-09-17 17:53:15 浏览:571
php考试系统源码 发布:2025-09-17 17:52:27 浏览:824
打牌的算法 发布:2025-09-17 17:37:46 浏览:174
ftp和ssh什么意思 发布:2025-09-17 16:46:18 浏览:501