Android多种方法获取系统时间
16lz
2021-01-23
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:2701-23 14:14:27.986: D/wxx(12455): DateFormat的12小时制:2016-01-23 02:14:2701-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的24小时制:2016-01-23 14:14:2701-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的12小时制:2016-01-23 02:14:2701-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的指定格式时间:16-01-2301-23 14:14:27.986: D/wxx(12455): SimpleDateFormat的指定格式时间:14:14:2701-23 14:14:27.986: D/wxx(12455): Calendar获得时间:2016-1-23, 14:14:2701-23 14:14:27.986: D/wxx(12455): Time获得时间:2016-1-23, 14:14:2701-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属性。
更多相关文章
- Android用户界面开发(9):日期和时间
- 从三流外包摇身变成阿里P6(Android开发岗),专科出生的他用五年时间
- Android UTC和Local 时间互转
- 【转】如何获取Android系统时间是24小时制还是12小时制
- android 获取root修改系统时间
- android实现一天24小时刻度尺
- android系统时间获取方式
- Android 修改系统时间代码