您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Android获取当前系统日期和时间

2025/11/22 12:45:26发布17次查看
本文用三种方法实现了在安卓上的时间获取功能,喜欢的小伙伴收藏一下,去试试吧!第一种方法simpledateformat simpledateformat = new simpledateformat("yyyy年mm月dd日 hh:mm:ss");// hh:mm:ss//获取当前时间date date = new date(system.currenttimemillis());time1.settext("date获取当前日期时间"+simpledateformat.format(date));
第二种方法
calendar calendar = calendar.getinstance();//获取系统的日期//年int year = calendar.get(calendar.year);//月int month = calendar.get(calendar.month)+1;//日int day = 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); time2.settext("calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
第三种方法
time t=new time(); // or time t=new time("gmt+8"); 加上time zone资料。t.settonow(); // 取得系统时间。int year = t.year;int month = t.month+1;int day = t.monthday;int hour = t.hour; // 0-23int minute = t.minute;int second = t.second;time3.settext("calendar获取当前日期"+year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
细心的可能发现月份要加上1,这个是因为是0开始计算的,所以用时要加上1。第一次接触android的可能最后一个看不懂,time1,time2 ,time3是textview的对象记得用时需要初始化下
textview time1 = (textview) findviewbyid(r.id.tv_time1);textview time2 = (textview) findviewbyid(r.id.tv_time2);textview time3 = (textview) findviewbyid(r.id.tv_time3);
准备发表还在考虑xml到底给你们贴出来不,还是贴上一个吧
<textview android:id="@+id/tv_time1" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:text="时间"/>
最后我运行到模拟器上了三种方法都可以,看个人习惯使用!
【推荐课程:android视频教程】
以上就是android获取当前系统日期和时间的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product