引用:http://www.itokit.com/2011/0517/66276.html
Android的文件有建议用Time代替Calendar。用Time对CPU的负荷会较小。在写Widget时特别重要。
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料。
来看下IT动力的完整代码吧:
- package itokit.com;
- import android.app.Activity;
- import android.os.Bundle;
- import android.text.format.Time;
- import android.widget.TextView;
- public class ShowTime extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- TextView myTextView = (TextView)findViewById(R.id.myTextView);
- Time time = new Time("GMT+8");
- time.setToNow();
- int year = time.year;
- int month = time.month;
- int day = time.monthDay;
- int minute = time.minute;
- int hour = time.hour;
- int sec = time.second;
- myTextView.setText("当前时间为:" + year +
- "年 " + month +
- "月 " + day +
- "日 " + hour +
- "时 " + minute +
- "分 " + sec +
- "秒");
- }
- }
唯一不足是取出时间只有24小时模式.
========================================================================================
long time=System.currentTimeMillis();
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小时:mHour=mCalendar.get(Calendar.HOUR);
取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
如何获取Android系统时间是24小时制还是12小时制
ContentResolver cv = this.getContentResolver();
String strTimeFormat = android.provider.Settings.System.getString(cv,
android.provider.Settings.System.TIME_12_24);
if(strTimeFormat.equals("24"))
{
Log.i("activity","24");
}
Calendar c = Calendar.getInstance();
取得系统日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
利用Calendar获取
Calendar c = Calendar.getInstance();
取得系统日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)
Calendar c = Calendar.getInstance();
取得系统日期:year = c.get(Calendar.YEAR)
month = c.grt(Calendar.MONTH)
day = c.get(Calendar.DAY_OF_MONTH)
取得系统时间:hour = c.get(Calendar.HOUR_OF_DAY);
minute = c.get(Calendar.MINUTE)