Android开发中获取系统时间的几种种方式

版权声明:本文为博主原创文章,未经博主允许不得转载 https://blog.csdn.net/qq_41508747/article/details/89511064

Android开发中获取系统时间的几种种方式

一.使用Calendar获取系统时间

Calendar获取系统时间首先要用Calendar.getInstance()函数获取一个实例,再为该实例设定时区(中国的时区为GMT+8:00),最后使用Calendar.get()函数获取时间的具体信息,如年,月,日,小时,分,秒,星期几。缺点是获得的这些时间信息都是独立的,如果要一起显示的话,还要组装起来凑成一个字符串,稍显麻烦。不过如果只需要单个时间信息,如星期几,这种方法是比较方便的。并且可以根据Calendar.AMPM属性判断当前是AM还是PM(0为AM,1为PM),然后根据需要显示12小时或24小时的时间。或者直接获得Calendar.HOUROF_DAY显示24小时的时间。

  • 代码

/*

* 使用Calendar获取系统时间

*/

Calendar calendars;

private void getTime4() {

calendars = Calendar.getInstance();

calendars.setTimeZone(TimeZone.getTimeZone("GMT+8:00"));


String year = String.valueOf(calendars.get(Calendar.YEAR));

String month = String.valueOf(calendars.get(Calendar.MONTH));

String day = String.valueOf(calendars.get(Calendar.DATE));

String hour = String.valueOf(calendars.get(Calendar.HOUR));

String min = String.valueOf(calendars.get(Calendar.MINUTE));

String second = String.valueOf(calendars.get(Calendar.SECOND));

Boolean isAm = calendars.get(Calendar.AM_PM)==1 ? true:false;

Boolean is24 = DateFormat.is24HourFormat(getApplication()) ?true:false;


Log.i("md", " 年:"+year+" 月: "+month+" 日:"+day+" 时: "+hour+" 分: "+min+" 秒: "+second +" 是上午吗? "+isAm+" 是24小时制吗? "+is24);

}

二.使用date获取系统时间

Date方法比较简单,只需要一条语句:Date().toLocaleString(),就可以获得整个的时间信息,并且格式规范,不用再组装,可以直接显示。缺点是如果想用另外一种格式显示,或者只需要单个的时间信息,就比较麻烦。可以定义SimpleDateFormat,规定哪些信息显示,哪些信息不显示,如显示年、月、日、小时、分钟、星期几,可以定义下面的SimpleDateFormat:

01-01 03:18:24.326: I/md(17508): 年:2015 月: 0 日:1 时: 3 分: 18 秒: 24 是上午吗? false 是24小时制吗? true

代码

Date date = new Date();

String time = date.toLocaleString();

Log.i("md", "时间time为: "+time);

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒 E");

String sim = dateFormat.format(date);

Log.i("md", "时间sim为: "+sim);

三.使用currentTimeMills获取系统时间

1秒=1000毫秒(ms) 1毫秒=1/1,000秒(s)1秒=1,000,000 微秒(μs) 1微秒=1/1,000,000秒(s)1秒=1,000,000,000 纳秒(ns) 1纳秒=1/1,000,000,000秒(s)1秒=1,000,000,000,000 皮秒(ps) 1皮秒=1/1,000,000,000,000秒(s)1小时=60分钟=3600秒

01-01 03:31:31.458: I/md(18530): 时间time为: Jan 1, 2015 3:31:31 AM

01-01 03:31:31.459: I/md(18530): 时间sim为: 2015年-01月01日-03时31分31秒 Thu

代码

long currentTime = System.currentTimeMillis();

SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");

Date date = new Date(totalMillisecounds);

String sim = formatter.format(date);

Log.i("md", "当前时间:" +sim);

 

拓展

在开发过程中,通常很多人都习惯使用new Date()来获取当前时间。new Date()所做的事情其实就是调用了System.currentTimeMillis()。如果仅仅是需要或者毫秒数,那么完全可以使用System.currentTimeMillis()去代替new Date(),效率上会高一点。如果需要在同一个方法里面多次使用new Date(),通常性能就是这样一点一点地消耗掉,这里其实可以声明一个引用。1分钟=60秒

01-01 03:18:24.316: I/md(17508): 当前时间:2015年-01月01日-03时18分24秒

代码

//获得系统的时间,单位为毫秒,转换为妙

long totalMilliSeconds = System.currentTimeMillis();


DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.MEDIUM);//格式化输出

TimeZone timeZoneChina = TimeZone.getTimeZone("Asia/Shanghai");//获取时区 这句加上,很关键。

dateFormatterChina.setTimeZone(timeZoneChina);//设置系统时区

long totalSeconds = totalMilliSeconds / 1000;


//求出现在的秒

long currentSecond = totalSeconds % 60;

//求出现在的分

long totalMinutes = totalSeconds / 60;

long currentMinute = totalMinutes % 60;

//求出现在的小时

long totalHour = totalMinutes / 60;

long currentHour = totalHour % 24;

Log.i("md", "小时:"+currentHour+" 分钟: "+currentMinute+" 秒 :"+currentSecond);

猜你喜欢

转载自blog.csdn.net/qq_41508747/article/details/89511064