Android 系统时间日期的获取
来源:互联网 发布:大学生借款软件 编辑:程序博客网 时间:2024/05/21 17:23
android项目中有时候需要获取到当前的系统时间和日期
出于习惯,我们可能会这样
new Date(System.currentTimeMillis());
但是事实上,这样做之后,很可能跟实际上的时间相差十万八千里
其实还有一个常用的方法就是通过Calendar类
Calendar c = Calendar.getInstance();new Date(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));
Calendar.DATE 等同于 Calendar.DAY_OF_MONTH
date.getDay(); // 返回年日期date.getMonth(); // 返回月日期date.getDay(); // 返回日// 但是,真的是这样吗
如果仔细看看文档的话,你会发现其实返回的是 the day of the week,就是星期内的天数
所以,想要获得day数据的话,需要用到
date.getDate(); // 返回the day of the month.
还有一个方法,就是android.text.format.Time
public static Time getCurrentTime() {Time time = new Time();time.setToNow();Log.v(TAG, "cureent time: " + time.year + "-" + (time.month + 1) + "-" + time.monthDay+ " " + time.hour + ":" + time.minute + ":" + time.second);return time;}
time.setToNow() 这句话就是拿到系统当前的时间,time里面包含了year,month,monthDay,yearDay,hours,miniute,seconds等所有常用的字段,非常方便
PS:有一点需要注意的就是,在java里面,不论是Calendar,Date还是Time,关于month这个字段的数据,如果不是手动设置的话,你会发现返回的数据总会比实际月份小1
我认为是,month这个数据反映的其实不是实际上几月份,而是当前月在一年中排到的第几个月,别忘了,java里索引是从0开始的哦
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- Android 系统时间日期的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- Android系统日期时间的获取
- android 中 系统日期时间的获取
- android 中 系统日期时间的获取
- 网络流 练习
- Zirco-browser:超越海豚的开源浏览器 http://www.apkbus.com/android-44306-1-1.html (出处: Android开发论坛 - 安卓开发论坛 - An
- Oracle 11g R2( + RAC) 安装 系统用户组 配置 说明
- Smarty的常用东东
- Python开发技术详解-笔记_第06章-字符串与正则表达式
- Android 系统时间日期的获取
- 在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据
- Nginx 启动、停止、平滑重启
- MonetDB——集群
- AOJ 2308 —— 计算几何
- 7.C语言之字符串操作函数
- win7出现任务栏不见了,各个进程莫名被终止解决办法
- 让TOMCAT支持软连接
- JAVA网页浏览器