Android多種方法獲取系統時間
阿新 • • 發佈:2019-01-30
Android中獲取系統時間非常簡單,也很常用。其中有幾種方法都可以實現,但每種有點區別,或有些需要注意的。在這裡我說幾點自己遇到的,權當筆記總結。
不扯蛋了, 直接上自己實現的測試程式碼:
package com.wuxianxi.test;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.util.Log;
public class MainActivity extends ActionBarActivity {
private static final String TAG = "wxx";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneTime();
twoTime();
threeTime();
fourTime();
fiveTime();
sixTime();
sevenTime();
eightTime();
nineTime();
}
private void oneTime() {
String time = DateFormat.format("yyyy-MM-dd HH:mm:ss", new Date())
.toString();
Log.d(TAG, "DateFormat的24小時制:" + time);
}
private void twoTime() {
String time = DateFormat.format("yyyy-MM-dd hh:mm:ss", new Date())
.toString();
Log.d(TAG, "DateFormat的12小時制:" + time);
}
private void threeTime() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = format.format(date);
Log.d(TAG, "SimpleDateFormat的24小時制:" + time);
}
private void fourTime() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String time = format.format(date);
Log.d(TAG, "SimpleDateFormat的12小時制:" + time);
}
private void fiveTime() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd");
String time = format.format(date);
Log.d(TAG, "SimpleDateFormat的指定格式時間:" + time);
}
private void sixTime() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
String time = format.format(date);
Log.d(TAG, "SimpleDateFormat的指定格式時間:" + time);
}
private void sevenTime() {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH) + 1; //預設0~11,需要加1
int day = c.get(Calendar.DAY_OF_MONTH);
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
int second = c.get(Calendar.SECOND);
Log.d(TAG, "Calendar獲得時間:" + year + "-" + month + "-" + day + ", "
+ hour + ":" + minute + ":" + second);
}
private void eightTime() {
Time t = new Time();
t.setToNow(); // 得到系統時間
int year = t.year;
int month = t.month + 1; //預設0~11,需要加1
int day = t.monthDay;
int hour = t.hour;
int minute = t.minute;
int second = t.second;
Log.d(TAG, "Time獲得時間:" + year + "-" + month + "-" + day + ", " + hour
+ ":" + minute + ":" + second);
}
private void nineTime() {
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("GMT+08"));
String time = format.format(date);
Log.d(TAG, "時區TimeZone獲得時間:" + time);
}
}
執行列印日誌如下:
01-23 14:14:27.986: D/wxx(12455): DateFormat的24小時制:2016-01-23 14:14:27
01-23 14:14:27.986: D/wxx(12455): DateFormat的12小時制:2016-01-23 02:14:27
01-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的24小時制:2016-01-23 14:14:27
01-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的12小時制:2016-01-23 02:14:27
01-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的指定格式時間:16-01-23
01-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的指定格式時間:14:14:27
01-23 14:14:27.986: D/wxx(12455): Calendar獲得時間:2016-1-23, 14:14:27
01-23 14:14:27.986: D/wxx(12455): Time獲得時間:2016-1-23, 14:14:27
01-23 14:14:27.986: D/wxx(12455): 時區TimeZone獲得時間:2016-01-23 14:14:27
不過分析了, 程式碼簡單,一看就懂。。下面就說幾點總結和注意要點:
1。可用DateFormat/ SimpleDateFormat/ Calendar/ Time幾種形式獲取系統時間;
2。大寫MM代表月份,小寫mm代表分鐘;大寫HH代表獲得時間是24小時制的,小寫hh代表時間是12小時制的;
3。Calendar/ Time 得到的月份是0~11,所以需要加1才是真正月份;
4。時區要設定TimeZone屬性。