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 (转载请说明)
时间: 2024-11-08 13:04:17