安卓开发

不积跬步无以至千里,不积小流无以成江海

Android 安卓系统时间获取

获取当前系统时间的方法:

1、用Calendar获取:

Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);            //年份
int month = calendar.get(Calendar.MONTH);          //月份
int date = calendar.get(Calendar.DATE);            //日期
int hour = calendar.get(Calendar.HOUR_OF_DAY);     //小时
int minute = calendar.get(Calendar.MINUTE);        //分钟
int second = calendar.get(Calendar.SECOND);        //秒

2、用Time获取:

Time time = new Time();
int yearDay = time.yearDay;    //Day of year [0-365]
int weekDay  = time.weekDay;   //Day of week [0-6]        
int year = time.year;          //Year. For example, 1970.
int month = time.month;        //Month [0-11]
int monthDay = time.monthDay;  //Day of month [1-31]
int hour = time.hour;          //Hour of day [0-23]
int minute = time.minute;      //Minute [0-59]
int second = time.second;      //Seconds [0-61] (2 leap seconds allowed)


缺陷:存在“Year_2038_problem”问题,导致获取到的时间可能不可靠,已不使用,不推荐。具体可查看安卓源码说明。


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yuqi007163/article/details/52367222
个人分类: 时钟
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭