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属性。

更多相关文章

  1. 干货 | 聊聊这些年总结的一些学习方法
  2. Android(安卓)MP4取得播放时长的方法
  3. Android(安卓)轮询最佳实践 Service + AlarmManager
  4. android性能优化之布局优化
  5. 【Android(安卓)开发】: Android(安卓)消息处理机制之三: Handle
  6. 安卓不知道怎么学?看十年码农如何回答这个问题。
  7. Android用户界面开发(9):日期和时间
  8. Android计算优化解析
  9. 如何使Android应用程序获得root权限

随机推荐

  1. 将 android sql文件放生成在sd卡
  2. android读写串口
  3. Android InstrumentationTestRunner 链接
  4. Android: android x86 in VirtualBox
  5. android从网上加载图片简单示例
  6. 关于Android重力感应器的频率的分析
  7. android使用adb命令安装软件
  8. Android(安卓)游戏引擎libgdx 如何添加万
  9. android文件读取
  10. Android EditText保留小数点后两位