安卓--获取系统时间

  1.   SimpleDateFormat   formatter   =   new   SimpleDateFormat   ("yyyy年MM月dd日   HH:mm:ss");     
  2.   Date curDate =  new Date(System.currentTimeMillis());  

  1. //获取当前时间    
  2. String   str   =   formatter.format(curDate);  

  1. 取得系统时间  
  2. 1。  
  3. long time=System.currentTimeMillis();  
  4.    
  5. 2。  
  6. final Calendar mCalendar=Calendar.getInstance();  
  7. mCalendar.setTimeInMillis(time);  
  8. 取得小时:mHour=mCalendar.get(Calendar.HOUR);  
  9. 取得分钟:mMinuts=mCalendar.get(Calendar.MINUTE);  
  10.    
  11.    
  12. 3。  
  13. Time t=new Time(); // or Time t=new Time("GMT+8"); 加上Time Zone资料  
  14. t.setToNow(); // 取得系统时间。  
  15. int year = t.year;  
  16. int month = t.month;  
  17. int date = t.monthDay;  
  18. int hour = t.hour;    // 0-23  
  19.    
  20. 4。  
  21. DateFormat df = new SimpleDateFormat("HH:mm:ss");  
  22. 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就是五分钟之后的时间


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值