Android 安卓系统时间获取

时间:2021-02-19 22:42:53

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

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”问题,导致获取到的时间可能不可靠,已不使用,不推荐。具体可查看安卓源码说明。