自己项目中有对功能的授权时间范围这一概念,所以要拿当前时间判断是否在授权时间范围内。
不多说了,直接上代码了
/**
* 判断当前时间是否在[startTime, endTime]区间
*
* @param nowTime 当前时间
* @param startTime 开始时间
* @param endTime 结束时间
* @return
*/
@SuppressLint("SimpleDateFormat")
public static boolean isEffectiveDate(String nowTime, String startTime, String endTime) {
String format = "yyyy-MM-dd HH:mm:ss";
Date nowDate = null;
Date startDate = null;
Date endDate = null;
try {
nowDate = new SimpleDateFormat(format).parse(nowTime);
startDate = new SimpleDateFormat(format).parse(startTime);
endDate = new SimpleDateFormat(format).parse(endTime);
long nowTimeLong = nowDate.getTime();
long startTimeLong = startDate.getTime();
long endTimeLong = endDate.getTime();
if (nowTimeLong == startTimeLong || nowTimeLong == endTimeLong) {
return true;
}
} catch (ParseException e) {
e.printStackTrace();
}
Calendar date = Calendar.getInstance();
date.setTime(nowDate);
Calendar begin = Calendar.getInstance();
begin.setTime(startDate);
Calendar end = Calendar.getInstance();
end.setTime(endDate);
return date.after(begin) && date.before(end);
}