您的位置:首页 > 移动开发 > Android开发

android获取当前系统时间,同时比较当前时间是否在一个时间段范围内

2016-05-13 18:37 543 查看
//获取当前系统时间
Date currentTime = new Date();//currentTime就是系统当前时间
//定义时间的格式
DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date strbeginDate = null;//起始时间
Date strendDate = null;//结束时间
try {
strbeginDate = fmt.parse(strBeginTime.toString());//将时间转化成相同格式的Date类型
strendDate = fmt.parse(strEndTime.toString());
} catch (ParseException e) {
e.printStackTrace();
}
if ((currentTime.getTime() - strbeginDate.getTime()) > 0 && (strendDate.getTime() - currentTime.getTime()) > 0) {//使用.getTime方法把时间转化成毫秒数,然后进行比较

ToastUtil.MyToast(UnlockActivity.this, "当前时间在范围内");
} else { ToastUtil.MyToast(UnlockActivity.this, "您的操作时间已到期,请重新申请操作时间"); }
//也可以得到连个时间之间的时间差,的醋的结果就是ms格式(毫秒数);
Long time=(currentTime.getTime() - strbeginDate.getTime());
System.out.println("时间的差值="+time);//可以把time/1000/60/60,就能得到小时数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: