Android獲取系統時間方法的總結


Android獲取系統時間方法的方法有很多種,常用的有Calendar、Date、currentTimeMills等方法。

(1)Calendar

Calendar獲取系統時間首先要用Calendar.getInstance()函數獲取一個實例,再為該實例設定時區(中國的時區為GMT+8:00),最后使用Calendar.get()函數獲取時間的具體信息,如年,月,日,小時,分,秒,星期幾。

package com.hzhi.time_example;

import java.util.Calendar;
import java.util.TimeZone;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	TextView txtTime1;
	TextView txtTime2;
	Calendar cal;
	String year;
	String month;
	String day;
	String hour;
	String minute;
	String second;
	String my_time_1;
	String my_time_2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txtTime1 = (TextView) findViewById(R.id.textView1);
		txtTime2 = (TextView) findViewById(R.id.textView2);
		
		cal = Calendar.getInstance(); 
		cal.setTimeZone(TimeZone.getTimeZone("GMT+8:00")); 
		
		year = String.valueOf(cal.get(Calendar.YEAR)); 
		month = String.valueOf(cal.get(Calendar.MONTH))+1;
		day = String.valueOf(cal.get(Calendar.DATE));
		if (cal.get(Calendar.AM_PM) == 0)
			hour = String.valueOf(cal.get(Calendar.HOUR));
		else
			hour = String.valueOf(cal.get(Calendar.HOUR)+12);
		minute = String.valueOf(cal.get(Calendar.MINUTE));
		second = String.valueOf(cal.get(Calendar.SECOND));
		
		my_time_1 = year + "-" + month + "-" + day;
		my_time_2 = hour + "-" + minute + "-" + second;
		
		txtTime1.setText(my_time_1);
		txtTime2.setText(my_time_2);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

缺點是獲得的這些時間信息都是獨立的,如果要一起顯示的話,還要組裝起來湊成一個字符串,稍顯麻煩。不過如果只需要單個時間信息,如星期幾,這種方法是比較方便的。並且可以根據Calendar.AM_PM屬性判斷當前是AM還是PM(0為AM,1為PM),然后根據需要顯示12小時或24小時的時間。或者直接獲得Calendar.HOUR_OF_DAY顯示24小時的時間。

(2)Date

Date方法比較簡單,只需要一條語句:Date().toLocaleString(),就可以獲得整個的時間信息,並且格式規范,不用再組裝,可以直接顯示。缺點是如果想用另外一種格式顯示,或者只需要單個的時間信息,就比較麻煩。

package com.hzhi.time_example_02;

import java.util.Date;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	TextView txt_time;
	Date dt;
	String str_time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		txt_time = (TextView) findViewById(R.id.textView1);
		dt = new Date();
		str_time = dt.toLocaleString();
		txt_time.setText(str_time);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}

可以定義SimpleDateFormat,規定哪些信息顯示,哪些信息不顯示,如顯示年、月、日、小時、分鍾、星期幾,可以定義下面的SimpleDateFormat:

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm E");
str_time = sdf.format(dt);
txt_time.setText(str_time);

(3)currentTimeMills

System.currentTimeMillis()產生一個毫秒數,即當前距離1970-01-01 00:00:00的毫秒數。如果只是得到這個毫秒數是毫無意義的,但是Date(System.currentTimeMillis())之后就可以把這個毫秒數轉換為一個日期變量。實際上,Date()就相當於Date(System.currentTimeMillis())。也可以在Date后面跟任意一個long型變量,如Date(1000000000),就得到一個日期:1970-01-12 21:46:40。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM