您的位置:首页 > 其它

安卓--获取系统时间

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个小时)

// 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就是五分钟之后的时间
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: