安卓--获取系统时间
2017-03-15 12:05
211 查看
SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
//获取当前时间
String str = formatter.format(curDate);
取得系统时间
1。
long time=System.currentTimeMillis();
2。
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小时:mHour=mCalendar.get(Calendar.HOUR);
取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
3。
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
4。
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
时间的加减法:(在返回的时间上加上3个小时)
Date curDate = new Date(System.currentTimeMillis());
//获取当前时间
String str = formatter.format(curDate);
取得系统时间
1。
long time=System.currentTimeMillis();
2。
final Calendar mCalendar=Calendar.getInstance();
mCalendar.setTimeInMillis(time);
取得小时:mHour=mCalendar.get(Calendar.HOUR);
取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);
3。
Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料
t.setToNow(); // 取得系统时间。
int year = t.year;
int month = t.month;
int date = t.monthDay;
int hour = t.hour; // 0-23
4。
DateFormat df = new SimpleDateFormat("HH:mm:ss");
df.format(new Date());
时间的加减法:(在返回的时间上加上3个小时)
// format对象是用来以指定的时间格式格式化时间的 SimpleDateFormat from = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 这里的格式可以自己设置 // format()方法是用来格式化时间的方法 Date d2 = from.parse(topic.getTopic_Date()); long a = d2.getTime() + 3 * 60 * 60 * 1000; Date b = new Date(a); String times = from.format(b);
判断两个时间的时间差:
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date curDate = new Date(System.currentTimeMillis());//获取当前时间 String str = formatter.format(curDate); Date d1 = formatter.parse(str); Date d2 = formatter.parse(topic.getTopic_Date()); long diff = d1.getTime() - d2.getTime();//这样得到的差值是微秒级别 long days = diff / (1000 * 60 * 60 * 24); long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
days = days * 24; hours = hours + days;
在系统时间的基础上加分钟数(2分钟为例):
//当前时间 Date d=new Date(); //转成long类型 long l=d.getTime(); //格式 SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm"); //5分钟之后 long a=l+2*60*1000; //转成字符串 final String s=sdf.format(a); //a就是五分钟之后的时间
相关文章推荐
- 安卓获取系统时间
- 关于安卓开始实现最简单地获取系统时间
- Android 安卓系统时间获取
- 安卓简单开发-获取系统时间
- 安卓小项目之Everyday(4)---分享功能、listview优化、获取系统时间
- 获取安卓系统时间
- 安卓编程之获取系统当前时间
- 安卓获取系统当前时间
- 获取安卓系统时间
- 安卓获取系统当前时间
- C++获取系统时间
- asp.net获取系统当前时间的方法详解
- C 标准库获取当前系统时间
- linux c 获取系统时间
- C语言获取系统本地时间和修改本地时间
- JAVA获取时间比系统时间少八个小时的解决办法
- 获取系统时间
- NSDate获取系统时间和NSCalendar获取系统日期的方法
- C++获取系统时间
- JAVA获取当前系统时间System.currentTimeMillis()