首页 > 代码库 > 关于安卓开始实现最简单地获取系统时间
关于安卓开始实现最简单地获取系统时间
1 package lianxi; 2 3 import com.example.jichu_lianxi.R; 4 5 import android.app.Activity; 6 7 import android.os.Bundle; 8 9 import android.text.format.Time; 10 11 import android.widget.TextView; 12 import android.widget.Toast;13 14 public class GetTime extends Activity { 15 16 /** Called when the activity is first created. */ 17 18 @Override 19 20 public void onCreate(Bundle savedInstanceState) { 21 22 super.onCreate(savedInstanceState); 23 24 setContentView(R.layout.gettime); 25 26 TextView myTextView = (TextView)findViewById(R.id.getTime); 27 Time time = new Time("GMT+8"); 28 time.setToNow(); 29 int year = time.year; //获取年份30 int month = time.month; //获取月份31 int day = time.monthDay; //获取日号32 int hour = time.hour; //获取小时,注意,这里的小时是24制的33 int minute = time.minute; //获取分钟34 int sec = time.second; //获取秒35 String time_s = "当前时间为:" + year + "年 " + month + "月 " + day + "日 " + hour + "时 " + minute + "分 " + sec + "秒"; 36 Toast.makeText(GetTime.this,time_s, Toast.LENGTH_SHORT).show();37 38 39 } 40 41 }
第二种方法:
package lianxi;import com.example.jichu_lianxi.R;import android.app.Activity; import android.os.Bundle; import android.text.format.Time; import android.widget.TextView; import android.widget.Toast;public class GetTime extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.gettime); Calendar c = Calendar.getInstance(); int year = c.get(Calendar.YEAR); //获取年份 int month = c.get(Calendar.MONTH); //获取月份 int day = c.get(Calendar.DAY_OF_MONTH); //获取日期 int hour = c.get(Calendar.HOUR_OF_DAY); //获取小时 int minute = c.get(Calendar.MINUTE); //获取分钟
String time_s = "当前时间为:" + year + "年 " + month + "月 " + day + "日 " + hour + "时 " + minute + "分 ";
Toast.makeText(GetTime.this,time_s, Toast.LENGTH_SHORT).show();} }
效果图:
参考于 http://www.01yun.com/mobile_development/20130425/378941.html (转载请说明)
关于安卓开始实现最简单地获取系统时间
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。