关于安卓开始实现最简单地获取系统时间
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 (转载请说明)
作者:听着music睡
出处:http://www.cnblogs.com/xqxacm/
Android交流群:38197636
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。